blob: 3733b6a776968fb13c1c67acbfe821f858a5520b [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 Lattnerb9c86512009-12-29 02:14:09 +0000416 setTailCall(CI.isTailCall());
417 setCallingConv(CI.getCallingConv());
418
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000419 Use *OL = OperandList;
420 Use *InOL = CI.OperandList;
421 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000422 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000423 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000424}
425
Devang Patel4c758ea2008-09-25 21:00:45 +0000426void CallInst::addAttribute(unsigned i, Attributes attr) {
427 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000428 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000429 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000430}
431
Devang Patel4c758ea2008-09-25 21:00:45 +0000432void CallInst::removeAttribute(unsigned i, Attributes attr) {
433 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000434 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000435 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000436}
437
Devang Patelba3fa6c2008-09-23 23:03:40 +0000438bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000439 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000440 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000441 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000442 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000443 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000444}
445
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000446/// IsConstantOne - Return true only if val is constant int 1
447static bool IsConstantOne(Value *val) {
448 assert(val && "IsConstantOne does not work with NULL val");
449 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
450}
451
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000452static Instruction *createMalloc(Instruction *InsertBefore,
453 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000454 const Type *AllocTy, Value *AllocSize,
455 Value *ArraySize, Function *MallocF,
456 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000457 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000458 "createMalloc needs either InsertBefore or InsertAtEnd");
459
460 // malloc(type) becomes:
461 // bitcast (i8* malloc(typeSize)) to type*
462 // malloc(type, arraySize) becomes:
463 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000464 if (!ArraySize)
465 ArraySize = ConstantInt::get(IntPtrTy, 1);
466 else if (ArraySize->getType() != IntPtrTy) {
467 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000468 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
469 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000470 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000471 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
472 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000474
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000475 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000476 if (IsConstantOne(AllocSize)) {
477 AllocSize = ArraySize; // Operand * 1 = Operand
478 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
479 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
480 false /*ZExt*/);
481 // Malloc arg is constant product of type size and array size
482 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
483 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000484 // Multiply type size by the array size...
485 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000486 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
487 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000488 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000489 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
490 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000491 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000492 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000493
Victor Hernandez788eaab2009-09-18 19:20:02 +0000494 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000495 // Create the call to Malloc.
496 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
497 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000498 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000499 Value *MallocFunc = MallocF;
500 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000501 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000502 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000503 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000504 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000505 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000506 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000507 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000508 Result = MCall;
509 if (Result->getType() != AllocPtrType)
510 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000511 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000512 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000513 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000514 Result = MCall;
515 if (Result->getType() != AllocPtrType) {
516 InsertAtEnd->getInstList().push_back(MCall);
517 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000518 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000519 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000520 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000521 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000522 if (Function *F = dyn_cast<Function>(MallocFunc)) {
523 MCall->setCallingConv(F->getCallingConv());
524 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
525 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000526 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000527
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000528 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000529}
530
531/// CreateMalloc - Generate the IR for a call to malloc:
532/// 1. Compute the malloc call's argument as the specified type's size,
533/// possibly multiplied by the array size if the array size is not
534/// constant 1.
535/// 2. Call malloc with that argument.
536/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000537Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
538 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000539 Value *AllocSize, Value *ArraySize,
540 const Twine &Name) {
541 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000542 ArraySize, NULL, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000543}
544
545/// CreateMalloc - Generate the IR for a call to malloc:
546/// 1. Compute the malloc call's argument as the specified type's size,
547/// possibly multiplied by the array size if the array size is not
548/// constant 1.
549/// 2. Call malloc with that argument.
550/// 3. Bitcast the result of the malloc call to the specified type.
551/// Note: This function does not add the bitcast to the basic block, that is the
552/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000553Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
554 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000555 Value *AllocSize, Value *ArraySize,
556 Function *MallocF, const Twine &Name) {
557 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000558 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000559}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000560
Victor Hernandeze2971492009-10-24 04:23:03 +0000561static Instruction* createFree(Value* Source, Instruction *InsertBefore,
562 BasicBlock *InsertAtEnd) {
563 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
564 "createFree needs either InsertBefore or InsertAtEnd");
565 assert(isa<PointerType>(Source->getType()) &&
566 "Can not free something of nonpointer type!");
567
568 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
569 Module* M = BB->getParent()->getParent();
570
571 const Type *VoidTy = Type::getVoidTy(M->getContext());
572 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
573 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000574 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000575 CallInst* Result = NULL;
576 Value *PtrCast = Source;
577 if (InsertBefore) {
578 if (Source->getType() != IntPtrTy)
579 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
580 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
581 } else {
582 if (Source->getType() != IntPtrTy)
583 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
584 Result = CallInst::Create(FreeFunc, PtrCast, "");
585 }
586 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000587 if (Function *F = dyn_cast<Function>(FreeFunc))
588 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000589
590 return Result;
591}
592
593/// CreateFree - Generate the IR for a call to the builtin free function.
594void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
595 createFree(Source, InsertBefore, NULL);
596}
597
598/// CreateFree - Generate the IR for a call to the builtin free function.
599/// Note: This function does not add the call to the basic block, that is the
600/// responsibility of the caller.
601Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
602 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
603 assert(FreeCall && "CreateFree did not create a CallInst");
604 return FreeCall;
605}
606
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607//===----------------------------------------------------------------------===//
608// InvokeInst Implementation
609//===----------------------------------------------------------------------===//
610
611void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000612 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000613 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000614 Use *OL = OperandList;
615 OL[0] = Fn;
616 OL[1] = IfNormal;
617 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000618 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000619 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000620 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000621
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000622 assert(((NumArgs == FTy->getNumParams()) ||
623 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000624 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000625
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000626 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000627 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000628 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000629 "Invoking a function with a bad signature!");
630
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000631 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000632 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633}
634
Misha Brukmanb1c93172005-04-21 23:48:37 +0000635InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000637 OperandTraits<InvokeInst>::op_end(this)
638 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000639 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000640 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000641 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000642 Use *OL = OperandList, *InOL = II.OperandList;
643 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000644 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000645 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000646}
647
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
649 return getSuccessor(idx);
650}
651unsigned InvokeInst::getNumSuccessorsV() const {
652 return getNumSuccessors();
653}
654void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
655 return setSuccessor(idx, B);
656}
657
Devang Patelba3fa6c2008-09-23 23:03:40 +0000658bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000659 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000660 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000661 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000662 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000663 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000664}
665
Devang Patel4c758ea2008-09-25 21:00:45 +0000666void InvokeInst::addAttribute(unsigned i, Attributes attr) {
667 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000668 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000669 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000670}
671
Devang Patel4c758ea2008-09-25 21:00:45 +0000672void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
673 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000674 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000675 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000676}
677
Duncan Sands5208d1a2007-11-28 17:07:01 +0000678
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000679//===----------------------------------------------------------------------===//
680// ReturnInst Implementation
681//===----------------------------------------------------------------------===//
682
Chris Lattner2195fc42007-02-24 00:55:48 +0000683ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000684 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000685 OperandTraits<ReturnInst>::op_end(this) -
686 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000687 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000688 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000689 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000690 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000691}
692
Owen Anderson55f1c092009-08-13 21:58:54 +0000693ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
694 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000695 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
696 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000697 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000698 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000699}
Owen Anderson55f1c092009-08-13 21:58:54 +0000700ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
701 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000702 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
703 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000704 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000705 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000706}
Owen Anderson55f1c092009-08-13 21:58:54 +0000707ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
708 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000709 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000710}
711
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000712unsigned ReturnInst::getNumSuccessorsV() const {
713 return getNumSuccessors();
714}
715
Devang Patelae682fb2008-02-26 17:56:20 +0000716/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
717/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000718void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000719 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720}
721
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000722BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000723 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 return 0;
725}
726
Devang Patel59643e52008-02-23 00:35:18 +0000727ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000728}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000729
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000730//===----------------------------------------------------------------------===//
731// UnwindInst Implementation
732//===----------------------------------------------------------------------===//
733
Owen Anderson55f1c092009-08-13 21:58:54 +0000734UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
735 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
736 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000737}
Owen Anderson55f1c092009-08-13 21:58:54 +0000738UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
739 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
740 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000741}
742
743
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744unsigned UnwindInst::getNumSuccessorsV() const {
745 return getNumSuccessors();
746}
747
748void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000749 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750}
751
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000752BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000753 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000754 return 0;
755}
756
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000757//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000758// UnreachableInst Implementation
759//===----------------------------------------------------------------------===//
760
Owen Anderson55f1c092009-08-13 21:58:54 +0000761UnreachableInst::UnreachableInst(LLVMContext &Context,
762 Instruction *InsertBefore)
763 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
764 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000765}
Owen Anderson55f1c092009-08-13 21:58:54 +0000766UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
767 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
768 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000769}
770
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771unsigned UnreachableInst::getNumSuccessorsV() const {
772 return getNumSuccessors();
773}
774
775void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000776 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000777}
778
779BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000780 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000781 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000782}
783
784//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785// BranchInst Implementation
786//===----------------------------------------------------------------------===//
787
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000788void BranchInst::AssertOK() {
789 if (isConditional())
Benjamin Kramera81a6df2010-01-05 20:07:06 +0000790 assert(getCondition()->getType()->isInteger(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000792}
793
Chris Lattner2195fc42007-02-24 00:55:48 +0000794BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000795 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000796 OperandTraits<BranchInst>::op_end(this) - 1,
797 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000798 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000799 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000800}
801BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
802 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000803 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000804 OperandTraits<BranchInst>::op_end(this) - 3,
805 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000806 Op<-1>() = IfTrue;
807 Op<-2>() = IfFalse;
808 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000809#ifndef NDEBUG
810 AssertOK();
811#endif
812}
813
814BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000815 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000816 OperandTraits<BranchInst>::op_end(this) - 1,
817 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000818 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000819 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000820}
821
822BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
823 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000824 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000825 OperandTraits<BranchInst>::op_end(this) - 3,
826 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000827 Op<-1>() = IfTrue;
828 Op<-2>() = IfFalse;
829 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000830#ifndef NDEBUG
831 AssertOK();
832#endif
833}
834
835
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000836BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000837 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000838 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
839 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000840 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000841 if (BI.getNumOperands() != 1) {
842 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000843 Op<-3>() = BI.Op<-3>();
844 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000845 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000846 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000847}
848
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000849
850Use* Use::getPrefix() {
851 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
852 if (PotentialPrefix.getOpaqueValue())
853 return 0;
854
855 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
856}
857
858BranchInst::~BranchInst() {
859 if (NumOperands == 1) {
860 if (Use *Prefix = OperandList->getPrefix()) {
861 Op<-1>() = 0;
862 //
863 // mark OperandList to have a special value for scrutiny
864 // by baseclass destructors and operator delete
865 OperandList = Prefix;
866 } else {
867 NumOperands = 3;
868 OperandList = op_begin();
869 }
870 }
871}
872
873
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000874BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
875 return getSuccessor(idx);
876}
877unsigned BranchInst::getNumSuccessorsV() const {
878 return getNumSuccessors();
879}
880void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
881 setSuccessor(idx, B);
882}
883
884
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000885//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000886// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887//===----------------------------------------------------------------------===//
888
Owen Andersonb6b25302009-07-14 23:09:55 +0000889static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000890 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000891 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000892 else {
893 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000894 "Passed basic block into allocation size parameter! Use other ctor");
Benjamin Kramera81a6df2010-01-05 20:07:06 +0000895 assert(Amt->getType()->isInteger(32) &&
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000896 "Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000897 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000898 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000899}
900
Victor Hernandez8acf2952009-10-23 21:09:37 +0000901AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
902 const Twine &Name, Instruction *InsertBefore)
903 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
904 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
905 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000906 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000907 setName(Name);
908}
909
910AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
911 const Twine &Name, BasicBlock *InsertAtEnd)
912 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
913 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
914 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000915 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000916 setName(Name);
917}
918
919AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
920 Instruction *InsertBefore)
921 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
922 getAISize(Ty->getContext(), 0), InsertBefore) {
923 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000924 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000925 setName(Name);
926}
927
928AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
929 BasicBlock *InsertAtEnd)
930 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
931 getAISize(Ty->getContext(), 0), InsertAtEnd) {
932 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000933 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000934 setName(Name);
935}
936
937AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
938 const Twine &Name, Instruction *InsertBefore)
939 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000940 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000941 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000942 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000943 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000944}
945
Victor Hernandez8acf2952009-10-23 21:09:37 +0000946AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
947 const Twine &Name, BasicBlock *InsertAtEnd)
948 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000949 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000950 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000951 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000952 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000953}
954
Gordon Henriksen14a55692007-12-10 02:14:30 +0000955// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000956AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000957}
958
Victor Hernandez8acf2952009-10-23 21:09:37 +0000959void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000960 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000961 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000962 assert(getAlignment() == Align && "Alignment representation error!");
963}
964
Victor Hernandez8acf2952009-10-23 21:09:37 +0000965bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000966 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
967 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000968 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000969}
970
Victor Hernandez8acf2952009-10-23 21:09:37 +0000971const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000972 return getType()->getElementType();
973}
974
Chris Lattner8b291e62008-11-26 02:54:17 +0000975/// isStaticAlloca - Return true if this alloca is in the entry block of the
976/// function and is a constant size. If so, the code generator will fold it
977/// into the prolog/epilog code, so it is basically free.
978bool AllocaInst::isStaticAlloca() const {
979 // Must be constant size.
980 if (!isa<ConstantInt>(getArraySize())) return false;
981
982 // Must be in the entry block.
983 const BasicBlock *Parent = getParent();
984 return Parent == &Parent->getParent()->front();
985}
986
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988// LoadInst Implementation
989//===----------------------------------------------------------------------===//
990
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000991void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000992 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000993 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000994}
995
Daniel Dunbar4975db62009-07-25 04:41:11 +0000996LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000997 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000998 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000999 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001000 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001001 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001002 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001003}
1004
Daniel Dunbar4975db62009-07-25 04:41:11 +00001005LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001006 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001007 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001008 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001009 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001010 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001011 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001012}
1013
Daniel Dunbar4975db62009-07-25 04:41:11 +00001014LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001015 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001016 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001017 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001018 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001019 setAlignment(0);
1020 AssertOK();
1021 setName(Name);
1022}
1023
Daniel Dunbar4975db62009-07-25 04:41:11 +00001024LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001025 unsigned Align, Instruction *InsertBef)
1026 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1027 Load, Ptr, InsertBef) {
1028 setVolatile(isVolatile);
1029 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001030 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001031 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001032}
1033
Daniel Dunbar4975db62009-07-25 04:41:11 +00001034LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001035 unsigned Align, BasicBlock *InsertAE)
1036 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1037 Load, Ptr, InsertAE) {
1038 setVolatile(isVolatile);
1039 setAlignment(Align);
1040 AssertOK();
1041 setName(Name);
1042}
1043
Daniel Dunbar4975db62009-07-25 04:41:11 +00001044LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001045 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001046 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001047 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001048 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001049 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +00001050 AssertOK();
1051 setName(Name);
1052}
1053
Daniel Dunbar27096822009-08-11 18:11:15 +00001054
1055
1056LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1057 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1058 Load, Ptr, InsertBef) {
1059 setVolatile(false);
1060 setAlignment(0);
1061 AssertOK();
1062 if (Name && Name[0]) setName(Name);
1063}
1064
1065LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1066 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1067 Load, Ptr, InsertAE) {
1068 setVolatile(false);
1069 setAlignment(0);
1070 AssertOK();
1071 if (Name && Name[0]) setName(Name);
1072}
1073
1074LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1075 Instruction *InsertBef)
1076: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1077 Load, Ptr, InsertBef) {
1078 setVolatile(isVolatile);
1079 setAlignment(0);
1080 AssertOK();
1081 if (Name && Name[0]) setName(Name);
1082}
1083
1084LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1085 BasicBlock *InsertAE)
1086 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1087 Load, Ptr, InsertAE) {
1088 setVolatile(isVolatile);
1089 setAlignment(0);
1090 AssertOK();
1091 if (Name && Name[0]) setName(Name);
1092}
1093
Christopher Lamb84485702007-04-22 19:24:39 +00001094void LoadInst::setAlignment(unsigned Align) {
1095 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001096 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1097 ((Log2_32(Align)+1)<<1));
Christopher Lamb84485702007-04-22 19:24:39 +00001098}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001099
1100//===----------------------------------------------------------------------===//
1101// StoreInst Implementation
1102//===----------------------------------------------------------------------===//
1103
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001104void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001105 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001106 assert(isa<PointerType>(getOperand(1)->getType()) &&
1107 "Ptr must have pointer type!");
1108 assert(getOperand(0)->getType() ==
1109 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001110 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001111}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001112
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001113
1114StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001115 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001116 OperandTraits<StoreInst>::op_begin(this),
1117 OperandTraits<StoreInst>::operands(this),
1118 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001119 Op<0>() = val;
1120 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001121 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001122 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001123 AssertOK();
1124}
1125
1126StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001127 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001128 OperandTraits<StoreInst>::op_begin(this),
1129 OperandTraits<StoreInst>::operands(this),
1130 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001131 Op<0>() = val;
1132 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001133 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001134 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001135 AssertOK();
1136}
1137
Misha Brukmanb1c93172005-04-21 23:48:37 +00001138StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001139 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001140 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001141 OperandTraits<StoreInst>::op_begin(this),
1142 OperandTraits<StoreInst>::operands(this),
1143 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001144 Op<0>() = val;
1145 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001146 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001147 setAlignment(0);
1148 AssertOK();
1149}
1150
1151StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1152 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001153 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001154 OperandTraits<StoreInst>::op_begin(this),
1155 OperandTraits<StoreInst>::operands(this),
1156 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001157 Op<0>() = val;
1158 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001159 setVolatile(isVolatile);
1160 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001161 AssertOK();
1162}
1163
Misha Brukmanb1c93172005-04-21 23:48:37 +00001164StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001165 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001166 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001167 OperandTraits<StoreInst>::op_begin(this),
1168 OperandTraits<StoreInst>::operands(this),
1169 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001170 Op<0>() = val;
1171 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001172 setVolatile(isVolatile);
1173 setAlignment(Align);
1174 AssertOK();
1175}
1176
1177StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001178 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001179 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001180 OperandTraits<StoreInst>::op_begin(this),
1181 OperandTraits<StoreInst>::operands(this),
1182 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001183 Op<0>() = val;
1184 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001185 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001186 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001187 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001188}
1189
Christopher Lamb84485702007-04-22 19:24:39 +00001190void StoreInst::setAlignment(unsigned Align) {
1191 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001192 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1193 ((Log2_32(Align)+1) << 1));
Christopher Lamb84485702007-04-22 19:24:39 +00001194}
1195
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001196//===----------------------------------------------------------------------===//
1197// GetElementPtrInst Implementation
1198//===----------------------------------------------------------------------===//
1199
Christopher Lambedf07882007-12-17 01:12:55 +00001200static unsigned retrieveAddrSpace(const Value *Val) {
1201 return cast<PointerType>(Val->getType())->getAddressSpace();
1202}
1203
Gabor Greif21ba1842008-06-06 20:28:12 +00001204void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001205 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001206 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1207 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001208 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001209
Chris Lattner79807c3d2007-01-31 19:47:18 +00001210 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001211 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001212
1213 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001214}
1215
Daniel Dunbar4975db62009-07-25 04:41:11 +00001216void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001217 assert(NumOperands == 2 && "NumOperands not initialized?");
1218 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001219 OL[0] = Ptr;
1220 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001221
1222 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001223}
1224
Gabor Greiff6caff662008-05-10 08:32:32 +00001225GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001226 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001227 OperandTraits<GetElementPtrInst>::op_end(this)
1228 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001229 GEPI.getNumOperands()) {
1230 Use *OL = OperandList;
1231 Use *GEPIOL = GEPI.OperandList;
1232 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001233 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001234 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001235}
1236
Chris Lattner82981202005-05-03 05:43:30 +00001237GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001238 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001239 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001240 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001241 GetElementPtr,
1242 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1243 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001244 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001245}
1246
1247GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001248 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001249 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001250 checkType(getIndexedType(Ptr->getType(),Idx)),
1251 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001252 GetElementPtr,
1253 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1254 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001255 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001256}
1257
Chris Lattner6090a422009-03-09 04:46:40 +00001258/// getIndexedType - Returns the type of the element that would be accessed with
1259/// a gep instruction with the specified parameters.
1260///
1261/// The Idxs pointer should point to a continuous piece of memory containing the
1262/// indices, either as Value* or uint64_t.
1263///
1264/// A null type is returned if the indices are invalid for the specified
1265/// pointer type.
1266///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001267template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001268static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1269 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001270 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1271 if (!PTy) return 0; // Type isn't a pointer type!
1272 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001273
Chris Lattner6090a422009-03-09 04:46:40 +00001274 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001275 if (NumIdx == 0)
1276 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001277
1278 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001279 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1280 // that contain opaque types) under the assumption that it will be resolved to
1281 // a sane type later.
1282 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001283 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001284
Dan Gohman1ecaf452008-05-31 00:58:22 +00001285 unsigned CurIdx = 1;
1286 for (; CurIdx != NumIdx; ++CurIdx) {
1287 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1288 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001289 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001290 if (!CT->indexValid(Index)) return 0;
1291 Agg = CT->getTypeAtIndex(Index);
1292
1293 // If the new type forwards to another type, then it is in the middle
1294 // of being refined to another type (and hence, may have dropped all
1295 // references to what it was using before). So, use the new forwarded
1296 // type.
1297 if (const Type *Ty = Agg->getForwardedType())
1298 Agg = Ty;
1299 }
1300 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001301}
1302
Matthijs Kooijman04468622008-07-29 08:46:11 +00001303const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1304 Value* const *Idxs,
1305 unsigned NumIdx) {
1306 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1307}
1308
1309const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1310 uint64_t const *Idxs,
1311 unsigned NumIdx) {
1312 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1313}
1314
Chris Lattner82981202005-05-03 05:43:30 +00001315const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1316 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1317 if (!PTy) return 0; // Type isn't a pointer type!
1318
1319 // Check the pointer index.
1320 if (!PTy->indexValid(Idx)) return 0;
1321
Chris Lattnerc2233332005-05-03 16:44:45 +00001322 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001323}
1324
Chris Lattner45f15572007-04-14 00:12:57 +00001325
1326/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1327/// zeros. If so, the result pointer and the first operand have the same
1328/// value, just potentially different types.
1329bool GetElementPtrInst::hasAllZeroIndices() const {
1330 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1331 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1332 if (!CI->isZero()) return false;
1333 } else {
1334 return false;
1335 }
1336 }
1337 return true;
1338}
1339
Chris Lattner27058292007-04-27 20:35:56 +00001340/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1341/// constant integers. If so, the result pointer and the first operand have
1342/// a constant offset between them.
1343bool GetElementPtrInst::hasAllConstantIndices() const {
1344 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1345 if (!isa<ConstantInt>(getOperand(i)))
1346 return false;
1347 }
1348 return true;
1349}
1350
Dan Gohman1b849082009-09-07 23:54:19 +00001351void GetElementPtrInst::setIsInBounds(bool B) {
1352 cast<GEPOperator>(this)->setIsInBounds(B);
1353}
Chris Lattner45f15572007-04-14 00:12:57 +00001354
Nick Lewycky28a5f252009-09-27 21:33:04 +00001355bool GetElementPtrInst::isInBounds() const {
1356 return cast<GEPOperator>(this)->isInBounds();
1357}
1358
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001359//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001360// ExtractElementInst Implementation
1361//===----------------------------------------------------------------------===//
1362
1363ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001364 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001365 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001366 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001367 ExtractElement,
1368 OperandTraits<ExtractElementInst>::op_begin(this),
1369 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001370 assert(isValidOperands(Val, Index) &&
1371 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001372 Op<0>() = Val;
1373 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001374 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001375}
1376
1377ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001378 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001379 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001380 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001381 ExtractElement,
1382 OperandTraits<ExtractElementInst>::op_begin(this),
1383 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001384 assert(isValidOperands(Val, Index) &&
1385 "Invalid extractelement instruction operands!");
1386
Gabor Greif2d3024d2008-05-26 21:33:52 +00001387 Op<0>() = Val;
1388 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001389 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001390}
1391
Chris Lattner65511ff2006-10-05 06:24:58 +00001392
Chris Lattner54865b32006-04-08 04:05:48 +00001393bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Benjamin Kramerd2564e32010-01-05 21:05:54 +00001394 if (!isa<VectorType>(Val->getType()) || !Index->getType()->isInteger(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001395 return false;
1396 return true;
1397}
1398
1399
Robert Bocchino23004482006-01-10 19:05:34 +00001400//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001401// InsertElementInst Implementation
1402//===----------------------------------------------------------------------===//
1403
Chris Lattner54865b32006-04-08 04:05:48 +00001404InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001405 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001406 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001407 : Instruction(Vec->getType(), InsertElement,
1408 OperandTraits<InsertElementInst>::op_begin(this),
1409 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001410 assert(isValidOperands(Vec, Elt, Index) &&
1411 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001412 Op<0>() = Vec;
1413 Op<1>() = Elt;
1414 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001415 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001416}
1417
Chris Lattner54865b32006-04-08 04:05:48 +00001418InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001419 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001420 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001421 : Instruction(Vec->getType(), InsertElement,
1422 OperandTraits<InsertElementInst>::op_begin(this),
1423 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001424 assert(isValidOperands(Vec, Elt, Index) &&
1425 "Invalid insertelement instruction operands!");
1426
Gabor Greif2d3024d2008-05-26 21:33:52 +00001427 Op<0>() = Vec;
1428 Op<1>() = Elt;
1429 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001430 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001431}
1432
Chris Lattner54865b32006-04-08 04:05:48 +00001433bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1434 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001435 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001436 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001437
Reid Spencerd84d35b2007-02-15 02:26:10 +00001438 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001439 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001440
Benjamin Kramerd2564e32010-01-05 21:05:54 +00001441 if (!Index->getType()->isInteger(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001442 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001443 return true;
1444}
1445
1446
Robert Bocchinoca27f032006-01-17 20:07:22 +00001447//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001448// ShuffleVectorInst Implementation
1449//===----------------------------------------------------------------------===//
1450
1451ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001452 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001453 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001454: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001455 cast<VectorType>(Mask->getType())->getNumElements()),
1456 ShuffleVector,
1457 OperandTraits<ShuffleVectorInst>::op_begin(this),
1458 OperandTraits<ShuffleVectorInst>::operands(this),
1459 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001460 assert(isValidOperands(V1, V2, Mask) &&
1461 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001462 Op<0>() = V1;
1463 Op<1>() = V2;
1464 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001465 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001466}
1467
1468ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001469 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001470 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001471: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1472 cast<VectorType>(Mask->getType())->getNumElements()),
1473 ShuffleVector,
1474 OperandTraits<ShuffleVectorInst>::op_begin(this),
1475 OperandTraits<ShuffleVectorInst>::operands(this),
1476 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001477 assert(isValidOperands(V1, V2, Mask) &&
1478 "Invalid shuffle vector instruction operands!");
1479
Gabor Greif2d3024d2008-05-26 21:33:52 +00001480 Op<0>() = V1;
1481 Op<1>() = V2;
1482 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001483 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001484}
1485
Mon P Wang25f01062008-11-10 04:46:22 +00001486bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001487 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001488 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001489 return false;
1490
1491 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1492 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Benjamin Kramerd2564e32010-01-05 21:05:54 +00001493 !MaskTy->getElementType()->isInteger(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001494 return false;
1495 return true;
1496}
1497
Chris Lattnerf724e342008-03-02 05:28:33 +00001498/// getMaskValue - Return the index from the shuffle mask for the specified
1499/// output result. This is either -1 if the element is undef or a number less
1500/// than 2*numelements.
1501int ShuffleVectorInst::getMaskValue(unsigned i) const {
1502 const Constant *Mask = cast<Constant>(getOperand(2));
1503 if (isa<UndefValue>(Mask)) return -1;
1504 if (isa<ConstantAggregateZero>(Mask)) return 0;
1505 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1506 assert(i < MaskCV->getNumOperands() && "Index out of range");
1507
1508 if (isa<UndefValue>(MaskCV->getOperand(i)))
1509 return -1;
1510 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1511}
1512
Dan Gohman12fce772008-05-15 19:50:34 +00001513//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001514// InsertValueInst Class
1515//===----------------------------------------------------------------------===//
1516
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001517void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001518 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001519 assert(NumOperands == 2 && "NumOperands not initialized?");
1520 Op<0>() = Agg;
1521 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001522
Dan Gohman1ecaf452008-05-31 00:58:22 +00001523 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001524 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001525}
1526
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001527void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001528 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001529 assert(NumOperands == 2 && "NumOperands not initialized?");
1530 Op<0>() = Agg;
1531 Op<1>() = Val;
1532
1533 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001534 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001535}
1536
1537InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001538 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001539 OperandTraits<InsertValueInst>::op_begin(this), 2),
1540 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001541 Op<0>() = IVI.getOperand(0);
1542 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001543 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001544}
1545
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001546InsertValueInst::InsertValueInst(Value *Agg,
1547 Value *Val,
1548 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001549 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001550 Instruction *InsertBefore)
1551 : Instruction(Agg->getType(), InsertValue,
1552 OperandTraits<InsertValueInst>::op_begin(this),
1553 2, InsertBefore) {
1554 init(Agg, Val, Idx, Name);
1555}
1556
1557InsertValueInst::InsertValueInst(Value *Agg,
1558 Value *Val,
1559 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001560 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001561 BasicBlock *InsertAtEnd)
1562 : Instruction(Agg->getType(), InsertValue,
1563 OperandTraits<InsertValueInst>::op_begin(this),
1564 2, InsertAtEnd) {
1565 init(Agg, Val, Idx, Name);
1566}
1567
Dan Gohman0752bff2008-05-23 00:36:11 +00001568//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001569// ExtractValueInst Class
1570//===----------------------------------------------------------------------===//
1571
Gabor Greifcbcc4952008-06-06 21:06:32 +00001572void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001573 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001574 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001575
Dan Gohman1ecaf452008-05-31 00:58:22 +00001576 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001577 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001578}
1579
Daniel Dunbar4975db62009-07-25 04:41:11 +00001580void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001581 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001582
1583 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001584 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001585}
1586
1587ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001588 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001589 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001590 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001591}
1592
Dan Gohman12fce772008-05-15 19:50:34 +00001593// getIndexedType - Returns the type of the element that would be extracted
1594// with an extractvalue instruction with the specified parameters.
1595//
1596// A null type is returned if the indices are invalid for the specified
1597// pointer type.
1598//
1599const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001600 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001601 unsigned NumIdx) {
1602 unsigned CurIdx = 0;
1603 for (; CurIdx != NumIdx; ++CurIdx) {
1604 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001605 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1606 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001607 if (!CT->indexValid(Index)) return 0;
1608 Agg = CT->getTypeAtIndex(Index);
1609
1610 // If the new type forwards to another type, then it is in the middle
1611 // of being refined to another type (and hence, may have dropped all
1612 // references to what it was using before). So, use the new forwarded
1613 // type.
1614 if (const Type *Ty = Agg->getForwardedType())
1615 Agg = Ty;
1616 }
1617 return CurIdx == NumIdx ? Agg : 0;
1618}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001619
Dan Gohman4a3125b2008-06-17 21:07:55 +00001620const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001621 unsigned Idx) {
1622 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001623}
1624
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001625//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001626// BinaryOperator Class
1627//===----------------------------------------------------------------------===//
1628
Dan Gohmana5b96452009-06-04 22:49:04 +00001629/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1630/// type is floating-point, to help provide compatibility with an older API.
1631///
1632static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1633 const Type *Ty) {
1634 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1635 if (Ty->isFPOrFPVector()) {
1636 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1637 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1638 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1639 }
1640 return iType;
1641}
1642
Chris Lattner2195fc42007-02-24 00:55:48 +00001643BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001644 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001645 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001646 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001647 OperandTraits<BinaryOperator>::op_begin(this),
1648 OperandTraits<BinaryOperator>::operands(this),
1649 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001650 Op<0>() = S1;
1651 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001652 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001653 setName(Name);
1654}
1655
1656BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001657 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001658 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001659 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001660 OperandTraits<BinaryOperator>::op_begin(this),
1661 OperandTraits<BinaryOperator>::operands(this),
1662 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001663 Op<0>() = S1;
1664 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001665 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001666 setName(Name);
1667}
1668
1669
1670void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001671 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001672 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001673 assert(LHS->getType() == RHS->getType() &&
1674 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001675#ifndef NDEBUG
1676 switch (iType) {
1677 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001678 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001679 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001680 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001681 assert(getType()->isIntOrIntVector() &&
1682 "Tried to create an integer operation on a non-integer type!");
1683 break;
1684 case FAdd: case FSub:
1685 case FMul:
1686 assert(getType() == LHS->getType() &&
1687 "Arithmetic operation should return same type as operands!");
1688 assert(getType()->isFPOrFPVector() &&
1689 "Tried to create a floating-point operation on a "
1690 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001691 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001692 case UDiv:
1693 case SDiv:
1694 assert(getType() == LHS->getType() &&
1695 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001696 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1697 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001698 "Incorrect operand type (not integer) for S/UDIV");
1699 break;
1700 case FDiv:
1701 assert(getType() == LHS->getType() &&
1702 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001703 assert(getType()->isFPOrFPVector() &&
1704 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001705 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001706 case URem:
1707 case SRem:
1708 assert(getType() == LHS->getType() &&
1709 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001710 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1711 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001712 "Incorrect operand type (not integer) for S/UREM");
1713 break;
1714 case FRem:
1715 assert(getType() == LHS->getType() &&
1716 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001717 assert(getType()->isFPOrFPVector() &&
1718 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001719 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001720 case Shl:
1721 case LShr:
1722 case AShr:
1723 assert(getType() == LHS->getType() &&
1724 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001725 assert((getType()->isInteger() ||
1726 (isa<VectorType>(getType()) &&
1727 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1728 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001729 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001730 case And: case Or:
1731 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001732 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001733 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001734 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001735 (isa<VectorType>(getType()) &&
1736 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001737 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001738 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001739 default:
1740 break;
1741 }
1742#endif
1743}
1744
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001745BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001746 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001747 Instruction *InsertBefore) {
1748 assert(S1->getType() == S2->getType() &&
1749 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001750 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001751}
1752
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001753BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001754 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001755 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001756 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001757 InsertAtEnd->getInstList().push_back(Res);
1758 return Res;
1759}
1760
Dan Gohman5476cfd2009-08-12 16:23:25 +00001761BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001762 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001763 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001764 return new BinaryOperator(Instruction::Sub,
1765 zero, Op,
1766 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001767}
1768
Dan Gohman5476cfd2009-08-12 16:23:25 +00001769BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001770 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001771 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001772 return new BinaryOperator(Instruction::Sub,
1773 zero, Op,
1774 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001775}
1776
Dan Gohman4ab44202009-12-18 02:58:50 +00001777BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1778 Instruction *InsertBefore) {
1779 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1780 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1781}
1782
1783BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1784 BasicBlock *InsertAtEnd) {
1785 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1786 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1787}
1788
Dan Gohman5476cfd2009-08-12 16:23:25 +00001789BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001790 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001791 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001792 return new BinaryOperator(Instruction::FSub,
1793 zero, Op,
1794 Op->getType(), Name, InsertBefore);
1795}
1796
Dan Gohman5476cfd2009-08-12 16:23:25 +00001797BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001798 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001799 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001800 return new BinaryOperator(Instruction::FSub,
1801 zero, Op,
1802 Op->getType(), Name, InsertAtEnd);
1803}
1804
Dan Gohman5476cfd2009-08-12 16:23:25 +00001805BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001806 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001807 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001808 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001809 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001810 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001811 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001812 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001813 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001814 }
1815
1816 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001817 Op->getType(), Name, InsertBefore);
1818}
1819
Dan Gohman5476cfd2009-08-12 16:23:25 +00001820BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001821 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001822 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001823 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001824 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001825 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001826 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001827 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001828 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001829 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001830 }
1831
1832 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001833 Op->getType(), Name, InsertAtEnd);
1834}
1835
1836
1837// isConstantAllOnes - Helper function for several functions below
1838static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001839 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1840 return CI->isAllOnesValue();
1841 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1842 return CV->isAllOnesValue();
1843 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001844}
1845
Owen Andersonbb2501b2009-07-13 22:18:28 +00001846bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001847 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1848 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001849 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1850 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001851 return false;
1852}
1853
Owen Andersonbb2501b2009-07-13 22:18:28 +00001854bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001855 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1856 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001857 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001858 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001859 return false;
1860}
1861
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001862bool BinaryOperator::isNot(const Value *V) {
1863 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1864 return (Bop->getOpcode() == Instruction::Xor &&
1865 (isConstantAllOnes(Bop->getOperand(1)) ||
1866 isConstantAllOnes(Bop->getOperand(0))));
1867 return false;
1868}
1869
Chris Lattner2c7d1772005-04-24 07:28:37 +00001870Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001871 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001872}
1873
Chris Lattner2c7d1772005-04-24 07:28:37 +00001874const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1875 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001876}
1877
Dan Gohmana5b96452009-06-04 22:49:04 +00001878Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001879 return cast<BinaryOperator>(BinOp)->getOperand(1);
1880}
1881
1882const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1883 return getFNegArgument(const_cast<Value*>(BinOp));
1884}
1885
Chris Lattner2c7d1772005-04-24 07:28:37 +00001886Value *BinaryOperator::getNotArgument(Value *BinOp) {
1887 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1888 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1889 Value *Op0 = BO->getOperand(0);
1890 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001891 if (isConstantAllOnes(Op0)) return Op1;
1892
1893 assert(isConstantAllOnes(Op1));
1894 return Op0;
1895}
1896
Chris Lattner2c7d1772005-04-24 07:28:37 +00001897const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1898 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001899}
1900
1901
1902// swapOperands - Exchange the two operands to this instruction. This
1903// instruction is safe to use on any binary instruction and does not
1904// modify the semantics of the instruction. If the instruction is
1905// order dependent (SetLT f.e.) the opcode is changed.
1906//
1907bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001908 if (!isCommutative())
1909 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001910 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001911 return false;
1912}
1913
Dan Gohman1b849082009-09-07 23:54:19 +00001914void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1915 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1916}
1917
1918void BinaryOperator::setHasNoSignedWrap(bool b) {
1919 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1920}
1921
1922void BinaryOperator::setIsExact(bool b) {
1923 cast<SDivOperator>(this)->setIsExact(b);
1924}
1925
Nick Lewycky28a5f252009-09-27 21:33:04 +00001926bool BinaryOperator::hasNoUnsignedWrap() const {
1927 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1928}
1929
1930bool BinaryOperator::hasNoSignedWrap() const {
1931 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1932}
1933
1934bool BinaryOperator::isExact() const {
1935 return cast<SDivOperator>(this)->isExact();
1936}
1937
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001938//===----------------------------------------------------------------------===//
1939// CastInst Class
1940//===----------------------------------------------------------------------===//
1941
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001942// Just determine if this cast only deals with integral->integral conversion.
1943bool CastInst::isIntegerCast() const {
1944 switch (getOpcode()) {
1945 default: return false;
1946 case Instruction::ZExt:
1947 case Instruction::SExt:
1948 case Instruction::Trunc:
1949 return true;
1950 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001951 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001952 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001953}
1954
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001955bool CastInst::isLosslessCast() const {
1956 // Only BitCast can be lossless, exit fast if we're not BitCast
1957 if (getOpcode() != Instruction::BitCast)
1958 return false;
1959
1960 // Identity cast is always lossless
1961 const Type* SrcTy = getOperand(0)->getType();
1962 const Type* DstTy = getType();
1963 if (SrcTy == DstTy)
1964 return true;
1965
Reid Spencer8d9336d2006-12-31 05:26:44 +00001966 // Pointer to pointer is always lossless.
1967 if (isa<PointerType>(SrcTy))
1968 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001969 return false; // Other types have no identity values
1970}
1971
1972/// This function determines if the CastInst does not require any bits to be
1973/// changed in order to effect the cast. Essentially, it identifies cases where
1974/// no code gen is necessary for the cast, hence the name no-op cast. For
1975/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001976/// # bitcast i32* %x to i8*
1977/// # bitcast <2 x i32> %x to <4 x i16>
1978/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001979/// @brief Determine if a cast is a no-op.
1980bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1981 switch (getOpcode()) {
1982 default:
1983 assert(!"Invalid CastOp");
1984 case Instruction::Trunc:
1985 case Instruction::ZExt:
1986 case Instruction::SExt:
1987 case Instruction::FPTrunc:
1988 case Instruction::FPExt:
1989 case Instruction::UIToFP:
1990 case Instruction::SIToFP:
1991 case Instruction::FPToUI:
1992 case Instruction::FPToSI:
1993 return false; // These always modify bits
1994 case Instruction::BitCast:
1995 return true; // BitCast never modifies bits.
1996 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001997 return IntPtrTy->getScalarSizeInBits() ==
1998 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001999 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002000 return IntPtrTy->getScalarSizeInBits() ==
2001 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002002 }
2003}
2004
2005/// This function determines if a pair of casts can be eliminated and what
2006/// opcode should be used in the elimination. This assumes that there are two
2007/// instructions like this:
2008/// * %F = firstOpcode SrcTy %x to MidTy
2009/// * %S = secondOpcode MidTy %F to DstTy
2010/// The function returns a resultOpcode so these two casts can be replaced with:
2011/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2012/// If no such cast is permited, the function returns 0.
2013unsigned CastInst::isEliminableCastPair(
2014 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
2015 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
2016{
2017 // Define the 144 possibilities for these two cast instructions. The values
2018 // in this matrix determine what to do in a given situation and select the
2019 // case in the switch below. The rows correspond to firstOp, the columns
2020 // correspond to secondOp. In looking at the table below, keep in mind
2021 // the following cast properties:
2022 //
2023 // Size Compare Source Destination
2024 // Operator Src ? Size Type Sign Type Sign
2025 // -------- ------------ ------------------- ---------------------
2026 // TRUNC > Integer Any Integral Any
2027 // ZEXT < Integral Unsigned Integer Any
2028 // SEXT < Integral Signed Integer Any
2029 // FPTOUI n/a FloatPt n/a Integral Unsigned
2030 // FPTOSI n/a FloatPt n/a Integral Signed
2031 // UITOFP n/a Integral Unsigned FloatPt n/a
2032 // SITOFP n/a Integral Signed FloatPt n/a
2033 // FPTRUNC > FloatPt n/a FloatPt n/a
2034 // FPEXT < FloatPt n/a FloatPt n/a
2035 // PTRTOINT n/a Pointer n/a Integral Unsigned
2036 // INTTOPTR n/a Integral Unsigned Pointer n/a
2037 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002038 //
2039 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002040 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2041 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002042 // of the produced value (we no longer know the top-part is all zeros).
2043 // Further this conversion is often much more expensive for typical hardware,
2044 // and causes issues when building libgcc. We disallow fptosi+sext for the
2045 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046 const unsigned numCastOps =
2047 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2048 static const uint8_t CastResults[numCastOps][numCastOps] = {
2049 // T F F U S F F P I B -+
2050 // R Z S P P I I T P 2 N T |
2051 // U E E 2 2 2 2 R E I T C +- secondOp
2052 // N X X U S F F N X N 2 V |
2053 // C T T I I P P C T T P T -+
2054 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2055 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2056 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002057 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2058 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2060 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2061 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2062 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2063 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2064 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2065 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2066 };
2067
2068 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2069 [secondOp-Instruction::CastOpsBegin];
2070 switch (ElimCase) {
2071 case 0:
2072 // categorically disallowed
2073 return 0;
2074 case 1:
2075 // allowed, use first cast's opcode
2076 return firstOp;
2077 case 2:
2078 // allowed, use second cast's opcode
2079 return secondOp;
2080 case 3:
2081 // no-op cast in second op implies firstOp as long as the DestTy
2082 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00002083 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002084 return firstOp;
2085 return 0;
2086 case 4:
2087 // no-op cast in second op implies firstOp as long as the DestTy
2088 // is floating point
2089 if (DstTy->isFloatingPoint())
2090 return firstOp;
2091 return 0;
2092 case 5:
2093 // no-op cast in first op implies secondOp as long as the SrcTy
2094 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00002095 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002096 return secondOp;
2097 return 0;
2098 case 6:
2099 // no-op cast in first op implies secondOp as long as the SrcTy
2100 // is a floating point
2101 if (SrcTy->isFloatingPoint())
2102 return secondOp;
2103 return 0;
2104 case 7: {
2105 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002106 if (!IntPtrTy)
2107 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002108 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2109 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110 if (MidSize >= PtrSize)
2111 return Instruction::BitCast;
2112 return 0;
2113 }
2114 case 8: {
2115 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2116 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2117 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002118 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2119 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002120 if (SrcSize == DstSize)
2121 return Instruction::BitCast;
2122 else if (SrcSize < DstSize)
2123 return firstOp;
2124 return secondOp;
2125 }
2126 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2127 return Instruction::ZExt;
2128 case 10:
2129 // fpext followed by ftrunc is allowed if the bit size returned to is
2130 // the same as the original, in which case its just a bitcast
2131 if (SrcTy == DstTy)
2132 return Instruction::BitCast;
2133 return 0; // If the types are not the same we can't eliminate it.
2134 case 11:
2135 // bitcast followed by ptrtoint is allowed as long as the bitcast
2136 // is a pointer to pointer cast.
2137 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2138 return secondOp;
2139 return 0;
2140 case 12:
2141 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2142 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2143 return firstOp;
2144 return 0;
2145 case 13: {
2146 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002147 if (!IntPtrTy)
2148 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002149 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2150 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2151 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002152 if (SrcSize <= PtrSize && SrcSize == DstSize)
2153 return Instruction::BitCast;
2154 return 0;
2155 }
2156 case 99:
2157 // cast combination can't happen (error in input). This is for all cases
2158 // where the MidTy is not the same for the two cast instructions.
2159 assert(!"Invalid Cast Combination");
2160 return 0;
2161 default:
2162 assert(!"Error in CastResults table!!!");
2163 return 0;
2164 }
2165 return 0;
2166}
2167
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002168CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002169 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002170 // Construct and return the appropriate CastInst subclass
2171 switch (op) {
2172 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2173 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2174 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2175 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2176 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2177 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2178 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2179 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2180 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2181 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2182 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2183 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2184 default:
2185 assert(!"Invalid opcode provided");
2186 }
2187 return 0;
2188}
2189
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002190CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002191 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002192 // Construct and return the appropriate CastInst subclass
2193 switch (op) {
2194 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2195 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2196 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2197 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2198 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2199 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2200 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2201 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2202 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2203 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2204 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2205 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2206 default:
2207 assert(!"Invalid opcode provided");
2208 }
2209 return 0;
2210}
2211
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002212CastInst *CastInst::CreateZExtOrBitCast(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::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002218}
2219
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002220CastInst *CastInst::CreateZExtOrBitCast(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::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002226}
2227
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002228CastInst *CastInst::CreateSExtOrBitCast(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::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002234}
2235
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002236CastInst *CastInst::CreateSExtOrBitCast(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::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002242}
2243
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002244CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002245 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002246 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002247 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002248 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2249 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002250}
2251
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002252CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002253 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002254 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002255 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002256 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2257 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002258}
2259
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002260CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002261 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002262 BasicBlock *InsertAtEnd) {
2263 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002264 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002265 "Invalid cast");
2266
Chris Lattner03c49532007-01-15 02:27:26 +00002267 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002268 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2269 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002270}
2271
2272/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002273CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002274 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002275 Instruction *InsertBefore) {
2276 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002277 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002278 "Invalid cast");
2279
Chris Lattner03c49532007-01-15 02:27:26 +00002280 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002281 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2282 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002283}
2284
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002285CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002286 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002287 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002288 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002289 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2290 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002291 Instruction::CastOps opcode =
2292 (SrcBits == DstBits ? Instruction::BitCast :
2293 (SrcBits > DstBits ? Instruction::Trunc :
2294 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002295 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002296}
2297
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002298CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002299 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002300 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002301 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2302 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002303 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2304 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002305 Instruction::CastOps opcode =
2306 (SrcBits == DstBits ? Instruction::BitCast :
2307 (SrcBits > DstBits ? Instruction::Trunc :
2308 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002309 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002310}
2311
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002312CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002313 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002314 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002315 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002316 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002317 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2318 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002319 Instruction::CastOps opcode =
2320 (SrcBits == DstBits ? Instruction::BitCast :
2321 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002322 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002323}
2324
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002325CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002326 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002327 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002328 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002329 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002330 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2331 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002332 Instruction::CastOps opcode =
2333 (SrcBits == DstBits ? Instruction::BitCast :
2334 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002335 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002336}
2337
Duncan Sands55e50902008-01-06 10:12:28 +00002338// Check whether it is valid to call getCastOpcode for these types.
2339// This routine must be kept in sync with getCastOpcode.
2340bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2341 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2342 return false;
2343
2344 if (SrcTy == DestTy)
2345 return true;
2346
2347 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002348 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2349 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002350
2351 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002352 if (DestTy->isInteger()) { // Casting to integral
2353 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002354 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002355 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002356 return true;
2357 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002358 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002359 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002360 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002361 return isa<PointerType>(SrcTy);
2362 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002363 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2364 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002365 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002366 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002367 return true;
2368 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002369 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002370 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002371 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002372 return false;
2373 }
2374 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002375 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002376 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002377 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002378 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002379 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002380 return DestPTy->getBitWidth() == SrcBits;
2381 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002382 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2383 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002384 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002385 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002386 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002387 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002388 return false;
2389 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002390 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002391 return false;
2392 }
2393}
2394
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002395// Provide a way to get a "cast" where the cast opcode is inferred from the
2396// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002397// logic in the castIsValid function below. This axiom should hold:
2398// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2399// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002401// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002403CastInst::getCastOpcode(
2404 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405 // Get the bit sizes, we'll need these
2406 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002407 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2408 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409
Duncan Sands55e50902008-01-06 10:12:28 +00002410 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2411 "Only first class types are castable!");
2412
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002414 if (DestTy->isInteger()) { // Casting to integral
2415 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416 if (DestBits < SrcBits)
2417 return Trunc; // int -> smaller int
2418 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002419 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 return SExt; // signed -> SEXT
2421 else
2422 return ZExt; // unsigned -> ZEXT
2423 } else {
2424 return BitCast; // Same size, No-op cast
2425 }
2426 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002427 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 return FPToSI; // FP -> sint
2429 else
2430 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002431 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002433 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002434 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 return BitCast; // Same size, no-op cast
2436 } else {
2437 assert(isa<PointerType>(SrcTy) &&
2438 "Casting from a value that is not first-class type");
2439 return PtrToInt; // ptr -> int
2440 }
2441 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002442 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002443 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 return SIToFP; // sint -> FP
2445 else
2446 return UIToFP; // uint -> FP
2447 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2448 if (DestBits < SrcBits) {
2449 return FPTrunc; // FP -> smaller FP
2450 } else if (DestBits > SrcBits) {
2451 return FPExt; // FP -> larger FP
2452 } else {
2453 return BitCast; // same size, no-op cast
2454 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002455 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002457 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002458 PTy = NULL;
2459 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002461 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002463 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2464 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002466 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002467 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002468 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002470 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002472 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473 }
2474 } else if (isa<PointerType>(DestTy)) {
2475 if (isa<PointerType>(SrcTy)) {
2476 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002477 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 return IntToPtr; // int -> ptr
2479 } else {
2480 assert(!"Casting pointer to other than pointer or int");
2481 }
2482 } else {
2483 assert(!"Casting to type that is not first-class");
2484 }
2485
2486 // If we fall through to here we probably hit an assertion cast above
2487 // and assertions are not turned on. Anything we return is an error, so
2488 // BitCast is as good a choice as any.
2489 return BitCast;
2490}
2491
2492//===----------------------------------------------------------------------===//
2493// CastInst SubClass Constructors
2494//===----------------------------------------------------------------------===//
2495
2496/// Check that the construction parameters for a CastInst are correct. This
2497/// could be broken out into the separate constructors but it is useful to have
2498/// it in one place and to eliminate the redundant code for getting the sizes
2499/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002500bool
2501CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502
2503 // Check for type sanity on the arguments
2504 const Type *SrcTy = S->getType();
2505 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2506 return false;
2507
2508 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002509 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2510 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511
2512 // Switch on the opcode provided
2513 switch (op) {
2514 default: return false; // This is an input error
2515 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002516 return SrcTy->isIntOrIntVector() &&
2517 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002519 return SrcTy->isIntOrIntVector() &&
2520 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002522 return SrcTy->isIntOrIntVector() &&
2523 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002525 return SrcTy->isFPOrFPVector() &&
2526 DstTy->isFPOrFPVector() &&
2527 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002529 return SrcTy->isFPOrFPVector() &&
2530 DstTy->isFPOrFPVector() &&
2531 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002534 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2535 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002536 return SVTy->getElementType()->isIntOrIntVector() &&
2537 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002538 SVTy->getNumElements() == DVTy->getNumElements();
2539 }
2540 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002541 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002544 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2545 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002546 return SVTy->getElementType()->isFPOrFPVector() &&
2547 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002548 SVTy->getNumElements() == DVTy->getNumElements();
2549 }
2550 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002551 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002553 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002554 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002555 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556 case Instruction::BitCast:
2557 // BitCast implies a no-op cast of type only. No bits change.
2558 // However, you can't cast pointers to anything but pointers.
2559 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2560 return false;
2561
Duncan Sands55e50902008-01-06 10:12:28 +00002562 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563 // these cases, the cast is okay if the source and destination bit widths
2564 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002565 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566 }
2567}
2568
2569TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002570 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002572 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573}
2574
2575TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002576 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002578 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579}
2580
2581ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002582 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002584 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585}
2586
2587ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002588 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002590 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591}
2592SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002593 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002595 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596}
2597
Jeff Cohencc08c832006-12-02 02:22:01 +00002598SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002599 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002601 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602}
2603
2604FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002605 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002607 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608}
2609
2610FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002611 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002613 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614}
2615
2616FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002617 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002619 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620}
2621
2622FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002623 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002625 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626}
2627
2628UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002629 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002631 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002632}
2633
2634UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002635 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002637 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002638}
2639
2640SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002641 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002643 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644}
2645
2646SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002647 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002649 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002650}
2651
2652FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002653 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002655 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002656}
2657
2658FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002659 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002661 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002662}
2663
2664FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002665 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002666) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002667 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002668}
2669
2670FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002671 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002672) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002673 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674}
2675
2676PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002677 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002678) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002679 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002680}
2681
2682PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002683 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002684) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002685 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002686}
2687
2688IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002689 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002690) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002691 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002692}
2693
2694IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002695 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002696) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002697 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002698}
2699
2700BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002701 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002702) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002703 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002704}
2705
2706BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002707 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002708) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002709 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002710}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002711
2712//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002713// CmpInst Classes
2714//===----------------------------------------------------------------------===//
2715
Nate Begemand2195702008-05-12 19:01:56 +00002716CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002717 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002718 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002719 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002720 OperandTraits<CmpInst>::op_begin(this),
2721 OperandTraits<CmpInst>::operands(this),
2722 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002723 Op<0>() = LHS;
2724 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002725 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002726 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002727}
Gabor Greiff6caff662008-05-10 08:32:32 +00002728
Nate Begemand2195702008-05-12 19:01:56 +00002729CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002730 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002731 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002732 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002733 OperandTraits<CmpInst>::op_begin(this),
2734 OperandTraits<CmpInst>::operands(this),
2735 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002736 Op<0>() = LHS;
2737 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002738 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002739 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002740}
2741
2742CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002743CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002744 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002745 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002746 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002747 if (InsertBefore)
2748 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2749 S1, S2, Name);
2750 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002751 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002752 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002753 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002754
2755 if (InsertBefore)
2756 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2757 S1, S2, Name);
2758 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002759 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002760 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002761}
2762
2763CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002764CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002765 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002766 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002767 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2768 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002769 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002770 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2771 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002772}
2773
2774void CmpInst::swapOperands() {
2775 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2776 IC->swapOperands();
2777 else
2778 cast<FCmpInst>(this)->swapOperands();
2779}
2780
2781bool CmpInst::isCommutative() {
2782 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2783 return IC->isCommutative();
2784 return cast<FCmpInst>(this)->isCommutative();
2785}
2786
2787bool CmpInst::isEquality() {
2788 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2789 return IC->isEquality();
2790 return cast<FCmpInst>(this)->isEquality();
2791}
2792
2793
Dan Gohman4e724382008-05-31 02:47:54 +00002794CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002795 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002796 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002797 case ICMP_EQ: return ICMP_NE;
2798 case ICMP_NE: return ICMP_EQ;
2799 case ICMP_UGT: return ICMP_ULE;
2800 case ICMP_ULT: return ICMP_UGE;
2801 case ICMP_UGE: return ICMP_ULT;
2802 case ICMP_ULE: return ICMP_UGT;
2803 case ICMP_SGT: return ICMP_SLE;
2804 case ICMP_SLT: return ICMP_SGE;
2805 case ICMP_SGE: return ICMP_SLT;
2806 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002807
Dan Gohman4e724382008-05-31 02:47:54 +00002808 case FCMP_OEQ: return FCMP_UNE;
2809 case FCMP_ONE: return FCMP_UEQ;
2810 case FCMP_OGT: return FCMP_ULE;
2811 case FCMP_OLT: return FCMP_UGE;
2812 case FCMP_OGE: return FCMP_ULT;
2813 case FCMP_OLE: return FCMP_UGT;
2814 case FCMP_UEQ: return FCMP_ONE;
2815 case FCMP_UNE: return FCMP_OEQ;
2816 case FCMP_UGT: return FCMP_OLE;
2817 case FCMP_ULT: return FCMP_OGE;
2818 case FCMP_UGE: return FCMP_OLT;
2819 case FCMP_ULE: return FCMP_OGT;
2820 case FCMP_ORD: return FCMP_UNO;
2821 case FCMP_UNO: return FCMP_ORD;
2822 case FCMP_TRUE: return FCMP_FALSE;
2823 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002824 }
2825}
2826
Reid Spencer266e42b2006-12-23 06:05:41 +00002827ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2828 switch (pred) {
2829 default: assert(! "Unknown icmp predicate!");
2830 case ICMP_EQ: case ICMP_NE:
2831 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2832 return pred;
2833 case ICMP_UGT: return ICMP_SGT;
2834 case ICMP_ULT: return ICMP_SLT;
2835 case ICMP_UGE: return ICMP_SGE;
2836 case ICMP_ULE: return ICMP_SLE;
2837 }
2838}
2839
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002840ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2841 switch (pred) {
2842 default: assert(! "Unknown icmp predicate!");
2843 case ICMP_EQ: case ICMP_NE:
2844 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2845 return pred;
2846 case ICMP_SGT: return ICMP_UGT;
2847 case ICMP_SLT: return ICMP_ULT;
2848 case ICMP_SGE: return ICMP_UGE;
2849 case ICMP_SLE: return ICMP_ULE;
2850 }
2851}
2852
Reid Spencer0286bc12007-02-28 22:00:54 +00002853/// Initialize a set of values that all satisfy the condition with C.
2854///
2855ConstantRange
2856ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2857 APInt Lower(C);
2858 APInt Upper(C);
2859 uint32_t BitWidth = C.getBitWidth();
2860 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002861 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002862 case ICmpInst::ICMP_EQ: Upper++; break;
2863 case ICmpInst::ICMP_NE: Lower++; break;
2864 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2865 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2866 case ICmpInst::ICMP_UGT:
2867 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2868 break;
2869 case ICmpInst::ICMP_SGT:
2870 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2871 break;
2872 case ICmpInst::ICMP_ULE:
2873 Lower = APInt::getMinValue(BitWidth); Upper++;
2874 break;
2875 case ICmpInst::ICMP_SLE:
2876 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2877 break;
2878 case ICmpInst::ICMP_UGE:
2879 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2880 break;
2881 case ICmpInst::ICMP_SGE:
2882 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2883 break;
2884 }
2885 return ConstantRange(Lower, Upper);
2886}
2887
Dan Gohman4e724382008-05-31 02:47:54 +00002888CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002889 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002890 default: assert(!"Unknown cmp predicate!");
2891 case ICMP_EQ: case ICMP_NE:
2892 return pred;
2893 case ICMP_SGT: return ICMP_SLT;
2894 case ICMP_SLT: return ICMP_SGT;
2895 case ICMP_SGE: return ICMP_SLE;
2896 case ICMP_SLE: return ICMP_SGE;
2897 case ICMP_UGT: return ICMP_ULT;
2898 case ICMP_ULT: return ICMP_UGT;
2899 case ICMP_UGE: return ICMP_ULE;
2900 case ICMP_ULE: return ICMP_UGE;
2901
Reid Spencerd9436b62006-11-20 01:22:35 +00002902 case FCMP_FALSE: case FCMP_TRUE:
2903 case FCMP_OEQ: case FCMP_ONE:
2904 case FCMP_UEQ: case FCMP_UNE:
2905 case FCMP_ORD: case FCMP_UNO:
2906 return pred;
2907 case FCMP_OGT: return FCMP_OLT;
2908 case FCMP_OLT: return FCMP_OGT;
2909 case FCMP_OGE: return FCMP_OLE;
2910 case FCMP_OLE: return FCMP_OGE;
2911 case FCMP_UGT: return FCMP_ULT;
2912 case FCMP_ULT: return FCMP_UGT;
2913 case FCMP_UGE: return FCMP_ULE;
2914 case FCMP_ULE: return FCMP_UGE;
2915 }
2916}
2917
Reid Spencer266e42b2006-12-23 06:05:41 +00002918bool CmpInst::isUnsigned(unsigned short predicate) {
2919 switch (predicate) {
2920 default: return false;
2921 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2922 case ICmpInst::ICMP_UGE: return true;
2923 }
2924}
2925
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002926bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002927 switch (predicate) {
2928 default: return false;
2929 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2930 case ICmpInst::ICMP_SGE: return true;
2931 }
2932}
2933
2934bool CmpInst::isOrdered(unsigned short predicate) {
2935 switch (predicate) {
2936 default: return false;
2937 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2938 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2939 case FCmpInst::FCMP_ORD: return true;
2940 }
2941}
2942
2943bool CmpInst::isUnordered(unsigned short predicate) {
2944 switch (predicate) {
2945 default: return false;
2946 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2947 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2948 case FCmpInst::FCMP_UNO: return true;
2949 }
2950}
2951
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002952bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2953 switch(predicate) {
2954 default: return false;
2955 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2956 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2957 }
2958}
2959
2960bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2961 switch(predicate) {
2962 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2963 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2964 default: return false;
2965 }
2966}
2967
2968
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002969//===----------------------------------------------------------------------===//
2970// SwitchInst Implementation
2971//===----------------------------------------------------------------------===//
2972
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002973void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002974 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002975 ReservedSpace = 2+NumCases*2;
2976 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002977 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002978
Gabor Greif2d3024d2008-05-26 21:33:52 +00002979 OperandList[0] = Value;
2980 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002981}
2982
Chris Lattner2195fc42007-02-24 00:55:48 +00002983/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2984/// switch on and a default destination. The number of additional cases can
2985/// be specified here to make memory allocation more efficient. This
2986/// constructor can also autoinsert before another instruction.
2987SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2988 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002989 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2990 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002991 init(Value, Default, NumCases);
2992}
2993
2994/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2995/// switch on and a default destination. The number of additional cases can
2996/// be specified here to make memory allocation more efficient. This
2997/// constructor also autoinserts at the end of the specified BasicBlock.
2998SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2999 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003000 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3001 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00003002 init(Value, Default, NumCases);
3003}
3004
Misha Brukmanb1c93172005-04-21 23:48:37 +00003005SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00003006 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00003007 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003008 Use *OL = OperandList, *InOL = SI.OperandList;
3009 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003010 OL[i] = InOL[i];
3011 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003012 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003013 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003014}
3015
Gordon Henriksen14a55692007-12-10 02:14:30 +00003016SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003017 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003018}
3019
3020
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003021/// addCase - Add an entry to the switch instruction...
3022///
Chris Lattner47ac1872005-02-24 05:32:09 +00003023void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003024 unsigned OpNo = NumOperands;
3025 if (OpNo+2 > ReservedSpace)
3026 resizeOperands(0); // Get more space!
3027 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003028 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003029 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003030 OperandList[OpNo] = OnVal;
3031 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003032}
3033
3034/// removeCase - This method removes the specified successor from the switch
3035/// instruction. Note that this cannot be used to remove the default
3036/// destination (successor #0).
3037///
3038void SwitchInst::removeCase(unsigned idx) {
3039 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003040 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3041
3042 unsigned NumOps = getNumOperands();
3043 Use *OL = OperandList;
3044
3045 // Move everything after this operand down.
3046 //
3047 // FIXME: we could just swap with the end of the list, then erase. However,
3048 // client might not expect this to happen. The code as it is thrashes the
3049 // use/def lists, which is kinda lame.
3050 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3051 OL[i-2] = OL[i];
3052 OL[i-2+1] = OL[i+1];
3053 }
3054
3055 // Nuke the last value.
3056 OL[NumOps-2].set(0);
3057 OL[NumOps-2+1].set(0);
3058 NumOperands = NumOps-2;
3059}
3060
3061/// resizeOperands - resize operands - This adjusts the length of the operands
3062/// list according to the following behavior:
3063/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003064/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003065/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3066/// 3. If NumOps == NumOperands, trim the reserved space.
3067///
3068void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003069 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003070 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003071 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003072 } else if (NumOps*2 > NumOperands) {
3073 // No resize needed.
3074 if (ReservedSpace >= NumOps) return;
3075 } else if (NumOps == NumOperands) {
3076 if (ReservedSpace == NumOps) return;
3077 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003078 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003079 }
3080
3081 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003082 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003083 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003084 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003085 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003086 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003087 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003088 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003089}
3090
3091
3092BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3093 return getSuccessor(idx);
3094}
3095unsigned SwitchInst::getNumSuccessorsV() const {
3096 return getNumSuccessors();
3097}
3098void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3099 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003100}
Chris Lattnerf22be932004-10-15 23:52:53 +00003101
Chris Lattner3ed871f2009-10-27 19:13:16 +00003102//===----------------------------------------------------------------------===//
3103// SwitchInst Implementation
3104//===----------------------------------------------------------------------===//
3105
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003106void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Chris Lattner6747b4c2009-10-29 05:53:32 +00003107 assert(Address && isa<PointerType>(Address->getType()) &&
3108 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003109 ReservedSpace = 1+NumDests;
3110 NumOperands = 1;
3111 OperandList = allocHungoffUses(ReservedSpace);
3112
3113 OperandList[0] = Address;
3114}
3115
3116
3117/// resizeOperands - resize operands - This adjusts the length of the operands
3118/// list according to the following behavior:
3119/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3120/// of operation. This grows the number of ops by 2 times.
3121/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3122/// 3. If NumOps == NumOperands, trim the reserved space.
3123///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003124void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003125 unsigned e = getNumOperands();
3126 if (NumOps == 0) {
3127 NumOps = e*2;
3128 } else if (NumOps*2 > NumOperands) {
3129 // No resize needed.
3130 if (ReservedSpace >= NumOps) return;
3131 } else if (NumOps == NumOperands) {
3132 if (ReservedSpace == NumOps) return;
3133 } else {
3134 return;
3135 }
3136
3137 ReservedSpace = NumOps;
3138 Use *NewOps = allocHungoffUses(NumOps);
3139 Use *OldOps = OperandList;
3140 for (unsigned i = 0; i != e; ++i)
3141 NewOps[i] = OldOps[i];
3142 OperandList = NewOps;
3143 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3144}
3145
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003146IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3147 Instruction *InsertBefore)
3148: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003149 0, 0, InsertBefore) {
3150 init(Address, NumCases);
3151}
3152
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003153IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3154 BasicBlock *InsertAtEnd)
3155: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003156 0, 0, InsertAtEnd) {
3157 init(Address, NumCases);
3158}
3159
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003160IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3161 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003162 allocHungoffUses(IBI.getNumOperands()),
3163 IBI.getNumOperands()) {
3164 Use *OL = OperandList, *InOL = IBI.OperandList;
3165 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3166 OL[i] = InOL[i];
3167 SubclassOptionalData = IBI.SubclassOptionalData;
3168}
3169
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003170IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003171 dropHungoffUses(OperandList);
3172}
3173
3174/// addDestination - Add a destination.
3175///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003176void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003177 unsigned OpNo = NumOperands;
3178 if (OpNo+1 > ReservedSpace)
3179 resizeOperands(0); // Get more space!
3180 // Initialize some new operands.
3181 assert(OpNo < ReservedSpace && "Growing didn't work!");
3182 NumOperands = OpNo+1;
3183 OperandList[OpNo] = DestBB;
3184}
3185
3186/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003187/// indirectbr instruction.
3188void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003189 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3190
3191 unsigned NumOps = getNumOperands();
3192 Use *OL = OperandList;
3193
3194 // Replace this value with the last one.
3195 OL[idx+1] = OL[NumOps-1];
3196
3197 // Nuke the last value.
3198 OL[NumOps-1].set(0);
3199 NumOperands = NumOps-1;
3200}
3201
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003202BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003203 return getSuccessor(idx);
3204}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003205unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003206 return getNumSuccessors();
3207}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003208void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003209 setSuccessor(idx, B);
3210}
3211
3212//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003213// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003214//===----------------------------------------------------------------------===//
3215
Chris Lattnerf22be932004-10-15 23:52:53 +00003216// Define these methods here so vtables don't get emitted into every translation
3217// unit that uses these classes.
3218
Devang Patel11cf3f42009-10-27 22:16:29 +00003219GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3220 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003221}
3222
Devang Patel11cf3f42009-10-27 22:16:29 +00003223BinaryOperator *BinaryOperator::clone_impl() const {
3224 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003225}
3226
Devang Patel11cf3f42009-10-27 22:16:29 +00003227FCmpInst* FCmpInst::clone_impl() const {
3228 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003229}
3230
Devang Patel11cf3f42009-10-27 22:16:29 +00003231ICmpInst* ICmpInst::clone_impl() const {
3232 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003233}
3234
Devang Patel11cf3f42009-10-27 22:16:29 +00003235ExtractValueInst *ExtractValueInst::clone_impl() const {
3236 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003237}
3238
Devang Patel11cf3f42009-10-27 22:16:29 +00003239InsertValueInst *InsertValueInst::clone_impl() const {
3240 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003241}
3242
Devang Patel11cf3f42009-10-27 22:16:29 +00003243AllocaInst *AllocaInst::clone_impl() const {
3244 return new AllocaInst(getAllocatedType(),
3245 (Value*)getOperand(0),
3246 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003247}
3248
Devang Patel11cf3f42009-10-27 22:16:29 +00003249LoadInst *LoadInst::clone_impl() const {
3250 return new LoadInst(getOperand(0),
3251 Twine(), isVolatile(),
3252 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003253}
3254
Devang Patel11cf3f42009-10-27 22:16:29 +00003255StoreInst *StoreInst::clone_impl() const {
3256 return new StoreInst(getOperand(0), getOperand(1),
3257 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003258}
3259
Devang Patel11cf3f42009-10-27 22:16:29 +00003260TruncInst *TruncInst::clone_impl() const {
3261 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003262}
3263
Devang Patel11cf3f42009-10-27 22:16:29 +00003264ZExtInst *ZExtInst::clone_impl() const {
3265 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003266}
3267
Devang Patel11cf3f42009-10-27 22:16:29 +00003268SExtInst *SExtInst::clone_impl() const {
3269 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003270}
3271
Devang Patel11cf3f42009-10-27 22:16:29 +00003272FPTruncInst *FPTruncInst::clone_impl() const {
3273 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003274}
3275
Devang Patel11cf3f42009-10-27 22:16:29 +00003276FPExtInst *FPExtInst::clone_impl() const {
3277 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003278}
3279
Devang Patel11cf3f42009-10-27 22:16:29 +00003280UIToFPInst *UIToFPInst::clone_impl() const {
3281 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003282}
3283
Devang Patel11cf3f42009-10-27 22:16:29 +00003284SIToFPInst *SIToFPInst::clone_impl() const {
3285 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003286}
3287
Devang Patel11cf3f42009-10-27 22:16:29 +00003288FPToUIInst *FPToUIInst::clone_impl() const {
3289 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003290}
3291
Devang Patel11cf3f42009-10-27 22:16:29 +00003292FPToSIInst *FPToSIInst::clone_impl() const {
3293 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003294}
3295
Devang Patel11cf3f42009-10-27 22:16:29 +00003296PtrToIntInst *PtrToIntInst::clone_impl() const {
3297 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003298}
3299
Devang Patel11cf3f42009-10-27 22:16:29 +00003300IntToPtrInst *IntToPtrInst::clone_impl() const {
3301 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003302}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003303
Devang Patel11cf3f42009-10-27 22:16:29 +00003304BitCastInst *BitCastInst::clone_impl() const {
3305 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003306}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003307
Devang Patel11cf3f42009-10-27 22:16:29 +00003308CallInst *CallInst::clone_impl() const {
3309 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003310}
3311
Devang Patel11cf3f42009-10-27 22:16:29 +00003312SelectInst *SelectInst::clone_impl() const {
3313 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003314}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003315
Devang Patel11cf3f42009-10-27 22:16:29 +00003316VAArgInst *VAArgInst::clone_impl() const {
3317 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003318}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003319
Devang Patel11cf3f42009-10-27 22:16:29 +00003320ExtractElementInst *ExtractElementInst::clone_impl() const {
3321 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003322}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003323
Devang Patel11cf3f42009-10-27 22:16:29 +00003324InsertElementInst *InsertElementInst::clone_impl() const {
3325 return InsertElementInst::Create(getOperand(0),
3326 getOperand(1),
3327 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003328}
3329
Devang Patel11cf3f42009-10-27 22:16:29 +00003330ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3331 return new ShuffleVectorInst(getOperand(0),
3332 getOperand(1),
3333 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003334}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003335
Devang Patel11cf3f42009-10-27 22:16:29 +00003336PHINode *PHINode::clone_impl() const {
3337 return new PHINode(*this);
3338}
3339
3340ReturnInst *ReturnInst::clone_impl() const {
3341 return new(getNumOperands()) ReturnInst(*this);
3342}
3343
3344BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003345 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003346 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003347}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003348
Devang Patel11cf3f42009-10-27 22:16:29 +00003349SwitchInst *SwitchInst::clone_impl() const {
3350 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003351}
3352
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003353IndirectBrInst *IndirectBrInst::clone_impl() const {
3354 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003355}
3356
3357
Devang Patel11cf3f42009-10-27 22:16:29 +00003358InvokeInst *InvokeInst::clone_impl() const {
3359 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003360}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003361
Devang Patel11cf3f42009-10-27 22:16:29 +00003362UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003363 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003364 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003365}
3366
Devang Patel11cf3f42009-10-27 22:16:29 +00003367UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003368 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003369 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003370}