blob: 3070241ab5e6eddc33fb35f2bb94d54207447b68 [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"
Devang Pateladd58652009-09-23 18:32:25 +000027
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000028using namespace llvm;
29
Chris Lattner3e13b8c2008-01-02 23:42:30 +000030//===----------------------------------------------------------------------===//
31// CallSite Class
32//===----------------------------------------------------------------------===//
33
Gabor Greif1b9921a2009-01-11 22:33:22 +000034#define CALLSITE_DELEGATE_GETTER(METHOD) \
35 Instruction *II(getInstruction()); \
36 return isCall() \
37 ? cast<CallInst>(II)->METHOD \
38 : cast<InvokeInst>(II)->METHOD
39
40#define CALLSITE_DELEGATE_SETTER(METHOD) \
41 Instruction *II(getInstruction()); \
42 if (isCall()) \
43 cast<CallInst>(II)->METHOD; \
44 else \
45 cast<InvokeInst>(II)->METHOD
46
Duncan Sands85fab3a2008-02-18 17:32:13 +000047CallSite::CallSite(Instruction *C) {
48 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000049 I.setPointer(C);
50 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000051}
Sandeep Patel68c5f472009-09-02 08:44:58 +000052CallingConv::ID CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000053 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000054}
Sandeep Patel68c5f472009-09-02 08:44:58 +000055void CallSite::setCallingConv(CallingConv::ID CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000056 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000057}
Devang Patel4c758ea2008-09-25 21:00:45 +000058const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000059 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000060}
Devang Patel4c758ea2008-09-25 21:00:45 +000061void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000062 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000063}
Devang Patelba3fa6c2008-09-23 23:03:40 +000064bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000065 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000066}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000067uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000068 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000069}
Duncan Sands38ef3a82007-12-03 20:06:50 +000070bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000071 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000072}
Duncan Sands78c88722008-07-08 08:38:44 +000073void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000074 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000075}
Duncan Sands38ef3a82007-12-03 20:06:50 +000076bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000077 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000078}
Duncan Sands78c88722008-07-08 08:38:44 +000079void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000080 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000081}
82bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000083 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000084}
85void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000086 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000087}
Duncan Sands3353ed02007-12-18 09:59:50 +000088bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000089 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000090}
Duncan Sandsaa31b922007-12-19 21:13:37 +000091void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000092 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000093}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000094
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000095bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000096 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
97 if (AI->get() == Arg)
98 return true;
99 return false;
100}
101
Gabor Greif1b9921a2009-01-11 22:33:22 +0000102#undef CALLSITE_DELEGATE_GETTER
103#undef CALLSITE_DELEGATE_SETTER
104
Gordon Henriksen14a55692007-12-10 02:14:30 +0000105//===----------------------------------------------------------------------===//
106// TerminatorInst Class
107//===----------------------------------------------------------------------===//
108
109// Out of line virtual method, so the vtable, etc has a home.
110TerminatorInst::~TerminatorInst() {
111}
112
Gabor Greiff6caff662008-05-10 08:32:32 +0000113//===----------------------------------------------------------------------===//
114// UnaryInstruction Class
115//===----------------------------------------------------------------------===//
116
Gordon Henriksen14a55692007-12-10 02:14:30 +0000117// Out of line virtual method, so the vtable, etc has a home.
118UnaryInstruction::~UnaryInstruction() {
119}
120
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000121//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000122// SelectInst Class
123//===----------------------------------------------------------------------===//
124
125/// areInvalidOperands - Return a string if the specified operands are invalid
126/// for a select operation, otherwise return null.
127const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
128 if (Op1->getType() != Op2->getType())
129 return "both values to select must have same type";
130
131 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
132 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000133 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000134 return "vector select condition element type must be i1";
135 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
136 if (ET == 0)
137 return "selected values for vector select must be vectors";
138 if (ET->getNumElements() != VT->getNumElements())
139 return "vector select requires selected vectors to have "
140 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000141 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000142 return "select condition must be i1 or <n x i1>";
143 }
144 return 0;
145}
146
147
148//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149// PHINode Class
150//===----------------------------------------------------------------------===//
151
152PHINode::PHINode(const PHINode &PN)
153 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000154 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000155 ReservedSpace(PN.getNumOperands()) {
156 Use *OL = OperandList;
157 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000158 OL[i] = PN.getOperand(i);
159 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000161 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162}
163
Gordon Henriksen14a55692007-12-10 02:14:30 +0000164PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000165 if (OperandList)
166 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000167}
168
169// removeIncomingValue - Remove an incoming value. This is useful if a
170// predecessor basic block is deleted.
171Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
172 unsigned NumOps = getNumOperands();
173 Use *OL = OperandList;
174 assert(Idx*2 < NumOps && "BB not in PHI node!");
175 Value *Removed = OL[Idx*2];
176
177 // Move everything after this operand down.
178 //
179 // FIXME: we could just swap with the end of the list, then erase. However,
180 // client might not expect this to happen. The code as it is thrashes the
181 // use/def lists, which is kinda lame.
182 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
183 OL[i-2] = OL[i];
184 OL[i-2+1] = OL[i+1];
185 }
186
187 // Nuke the last value.
188 OL[NumOps-2].set(0);
189 OL[NumOps-2+1].set(0);
190 NumOperands = NumOps-2;
191
192 // If the PHI node is dead, because it has zero entries, nuke it now.
193 if (NumOps == 2 && DeletePHIIfEmpty) {
194 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000195 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000196 eraseFromParent();
197 }
198 return Removed;
199}
200
201/// resizeOperands - resize operands - This adjusts the length of the operands
202/// list according to the following behavior:
203/// 1. If NumOps == 0, grow the operand list in response to a push_back style
204/// of operation. This grows the number of ops by 1.5 times.
205/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
206/// 3. If NumOps == NumOperands, trim the reserved space.
207///
208void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000209 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000210 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000211 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
213 } else if (NumOps*2 > NumOperands) {
214 // No resize needed.
215 if (ReservedSpace >= NumOps) return;
216 } else if (NumOps == NumOperands) {
217 if (ReservedSpace == NumOps) return;
218 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000219 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220 }
221
222 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000224 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000225 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000227 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228}
229
Nate Begemanb3923212005-08-04 23:24:19 +0000230/// hasConstantValue - If the specified PHI node always merges together the same
231/// value, return the value, otherwise return null.
232///
Dan Gohman22571482009-09-03 15:34:35 +0000233/// If the PHI has undef operands, but all the rest of the operands are
234/// some unique value, return that value if it can be proved that the
235/// value dominates the PHI. If DT is null, use a conservative check,
236/// otherwise use DT to test for dominance.
237///
238Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000239 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000240 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000241 if (getIncomingValue(0) != this) // not X = phi X
242 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000243 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000244 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000245
Nate Begemanb3923212005-08-04 23:24:19 +0000246 // Otherwise if all of the incoming values are the same for the PHI, replace
247 // the PHI node with the incoming value.
248 //
249 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000250 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000251 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000252 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000253 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000254 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000255 if (InVal && getIncomingValue(i) != InVal)
256 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000257 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000258 }
Nate Begemanb3923212005-08-04 23:24:19 +0000259
260 // The only case that could cause InVal to be null is if we have a PHI node
261 // that only has entries for itself. In this case, there is no entry into the
262 // loop, so kill the PHI.
263 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000264 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000265
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000266 // If we have a PHI node like phi(X, undef, X), where X is defined by some
267 // instruction, we cannot always return X as the result of the PHI node. Only
268 // do this if X is not an instruction (thus it must dominate the PHI block),
269 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000270 if (!HasUndefInput || !isa<Instruction>(InVal))
271 return InVal;
272
273 Instruction *IV = cast<Instruction>(InVal);
274 if (DT) {
275 // We have a DominatorTree. Do a precise test.
276 if (!DT->dominates(IV, this))
277 return 0;
278 } else {
279 // If it is in the entry block, it obviously dominates everything.
280 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
281 isa<InvokeInst>(IV))
282 return 0; // Cannot guarantee that InVal dominates this PHINode.
283 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000284
Nate Begemanb3923212005-08-04 23:24:19 +0000285 // All of the incoming values are the same, return the value now.
286 return InVal;
287}
288
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000289
290//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291// CallInst Implementation
292//===----------------------------------------------------------------------===//
293
Gordon Henriksen14a55692007-12-10 02:14:30 +0000294CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000295}
296
Chris Lattner054ba2c2007-02-13 00:58:44 +0000297void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000298 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
299 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000300 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301
Misha Brukmanb1c93172005-04-21 23:48:37 +0000302 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000304 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000305
Chris Lattner054ba2c2007-02-13 00:58:44 +0000306 assert((NumParams == FTy->getNumParams() ||
307 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000308 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000309 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000310 assert((i >= FTy->getNumParams() ||
311 FTy->getParamType(i) == Params[i]->getType()) &&
312 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000313 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000314 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315}
316
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000317void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000318 assert(NumOperands == 3 && "NumOperands not set up?");
319 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000320 OL[0] = Func;
321 OL[1] = Actual1;
322 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000323
324 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000326 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000328 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000329 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000331 assert((0 >= FTy->getNumParams() ||
332 FTy->getParamType(0) == Actual1->getType()) &&
333 "Calling a function with a bad signature!");
334 assert((1 >= FTy->getNumParams() ||
335 FTy->getParamType(1) == Actual2->getType()) &&
336 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337}
338
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000339void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000340 assert(NumOperands == 2 && "NumOperands not set up?");
341 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000342 OL[0] = Func;
343 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000344
345 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000347 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000349 assert((FTy->getNumParams() == 1 ||
350 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000352 assert((0 == FTy->getNumParams() ||
353 FTy->getParamType(0) == Actual->getType()) &&
354 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355}
356
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000357void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000358 assert(NumOperands == 1 && "NumOperands not set up?");
359 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000360 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000361
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000362 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000364 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000365
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000366 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000367}
368
Daniel Dunbar4975db62009-07-25 04:41:11 +0000369CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000370 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
372 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000373 Instruction::Call,
374 OperandTraits<CallInst>::op_end(this) - 2,
375 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000376 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000377 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378}
379
Daniel Dunbar4975db62009-07-25 04:41:11 +0000380CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381 BasicBlock *InsertAtEnd)
382 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
383 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000384 Instruction::Call,
385 OperandTraits<CallInst>::op_end(this) - 2,
386 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000387 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000388 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000390CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000391 Instruction *InsertBefore)
392 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
393 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000394 Instruction::Call,
395 OperandTraits<CallInst>::op_end(this) - 1,
396 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000397 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000398 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399}
400
Daniel Dunbar4975db62009-07-25 04:41:11 +0000401CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000402 BasicBlock *InsertAtEnd)
403 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
404 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000405 Instruction::Call,
406 OperandTraits<CallInst>::op_end(this) - 1,
407 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000408 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000409 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000410}
411
Misha Brukmanb1c93172005-04-21 23:48:37 +0000412CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000413 : Instruction(CI.getType(), Instruction::Call,
414 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000415 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000416 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000417 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000418 Use *OL = OperandList;
419 Use *InOL = CI.OperandList;
420 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000421 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000422 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000423}
424
Devang Patel4c758ea2008-09-25 21:00:45 +0000425void CallInst::addAttribute(unsigned i, Attributes attr) {
426 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000427 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000428 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000429}
430
Devang Patel4c758ea2008-09-25 21:00:45 +0000431void CallInst::removeAttribute(unsigned i, Attributes attr) {
432 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000433 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000434 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000435}
436
Devang Patelba3fa6c2008-09-23 23:03:40 +0000437bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000438 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000439 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000440 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000441 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000442 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000443}
444
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445/// IsConstantOne - Return true only if val is constant int 1
446static bool IsConstantOne(Value *val) {
447 assert(val && "IsConstantOne does not work with NULL val");
448 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
449}
450
Victor Hernandezb9f58992009-11-06 01:33:24 +0000451static Value *checkArraySize(Value *Amt, const Type *IntPtrTy) {
452 if (!Amt)
453 Amt = ConstantInt::get(IntPtrTy, 1);
454 else {
455 assert(!isa<BasicBlock>(Amt) &&
456 "Passed basic block into malloc size parameter! Use other ctor");
457 assert(Amt->getType() == IntPtrTy &&
458 "Malloc array size is not an intptr!");
459 }
460 return Amt;
461}
462
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000463static Instruction *createMalloc(Instruction *InsertBefore,
464 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezb9f58992009-11-06 01:33:24 +0000465 const Type *AllocTy, Value *ArraySize,
466 Function *MallocF, const Twine &NameStr) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000467 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000468 "createMalloc needs either InsertBefore or InsertAtEnd");
469
470 // malloc(type) becomes:
471 // bitcast (i8* malloc(typeSize)) to type*
472 // malloc(type, arraySize) becomes:
473 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezb9f58992009-11-06 01:33:24 +0000474 Value *AllocSize = ConstantExpr::getSizeOf(AllocTy);
475 AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize),
476 IntPtrTy);
477 ArraySize = checkArraySize(ArraySize, IntPtrTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000478
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000479 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000480 if (IsConstantOne(AllocSize)) {
481 AllocSize = ArraySize; // Operand * 1 = Operand
482 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
483 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
484 false /*ZExt*/);
485 // Malloc arg is constant product of type size and array size
486 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
487 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000488 // Multiply type size by the array size...
489 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000490 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
491 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000493 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
494 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000495 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000496 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000497
Victor Hernandez788eaab2009-09-18 19:20:02 +0000498 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000499 // Create the call to Malloc.
500 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
501 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000502 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000503 if (!MallocF)
504 // prototype malloc as "void *malloc(size_t)"
505 MallocF = cast<Function>(M->getOrInsertFunction("malloc", BPTy,
506 IntPtrTy, NULL));
507 if (!MallocF->doesNotAlias(0)) MallocF->setDoesNotAlias(0);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000508 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000509 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000510 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000511 if (InsertBefore) {
512 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000513 Result = MCall;
514 if (Result->getType() != AllocPtrType)
515 // Create a cast instruction to convert to the right type...
Victor Hernandezb9f58992009-11-06 01:33:24 +0000516 Result = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000517 } else {
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000518 MCall = CallInst::Create(MallocF, AllocSize, "malloccall");
519 Result = MCall;
520 if (Result->getType() != AllocPtrType) {
521 InsertAtEnd->getInstList().push_back(MCall);
522 // Create a cast instruction to convert to the right type...
Victor Hernandezb9f58992009-11-06 01:33:24 +0000523 Result = new BitCastInst(MCall, AllocPtrType, NameStr);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000524 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000525 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000526 MCall->setTailCall();
Victor Hernandezce997802009-11-06 21:43:21 +0000527 MCall->setCallingConv(MallocF->getCallingConv());
Daniel Dunbarb9189002009-09-11 22:07:31 +0000528 assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
529 "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000530
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000531 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000532}
533
534/// CreateMalloc - Generate the IR for a call to malloc:
535/// 1. Compute the malloc call's argument as the specified type's size,
536/// possibly multiplied by the array size if the array size is not
537/// constant 1.
538/// 2. Call malloc with that argument.
539/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000540Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
541 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezb9f58992009-11-06 01:33:24 +0000542 Value *ArraySize, const Twine &Name) {
543 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000544 ArraySize, NULL, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000545}
546
547/// CreateMalloc - Generate the IR for a call to malloc:
548/// 1. Compute the malloc call's argument as the specified type's size,
549/// possibly multiplied by the array size if the array size is not
550/// constant 1.
551/// 2. Call malloc with that argument.
552/// 3. Bitcast the result of the malloc call to the specified type.
553/// Note: This function does not add the bitcast to the basic block, that is the
554/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000555Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
556 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezb9f58992009-11-06 01:33:24 +0000557 Value *ArraySize, Function* MallocF,
558 const Twine &Name) {
559 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000560 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000561}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000562
Victor Hernandeze2971492009-10-24 04:23:03 +0000563static Instruction* createFree(Value* Source, Instruction *InsertBefore,
564 BasicBlock *InsertAtEnd) {
565 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
566 "createFree needs either InsertBefore or InsertAtEnd");
567 assert(isa<PointerType>(Source->getType()) &&
568 "Can not free something of nonpointer type!");
569
570 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
571 Module* M = BB->getParent()->getParent();
572
573 const Type *VoidTy = Type::getVoidTy(M->getContext());
574 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
575 // prototype free as "void free(void*)"
Victor Hernandezce997802009-11-06 21:43:21 +0000576 Function *FreeFunc = cast<Function>(M->getOrInsertFunction("free", VoidTy,
577 IntPtrTy, NULL));
Victor Hernandeze2971492009-10-24 04:23:03 +0000578 CallInst* Result = NULL;
579 Value *PtrCast = Source;
580 if (InsertBefore) {
581 if (Source->getType() != IntPtrTy)
582 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
583 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
584 } else {
585 if (Source->getType() != IntPtrTy)
586 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
587 Result = CallInst::Create(FreeFunc, PtrCast, "");
588 }
589 Result->setTailCall();
Victor Hernandezce997802009-11-06 21:43:21 +0000590 Result->setCallingConv(FreeFunc->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000591
592 return Result;
593}
594
595/// CreateFree - Generate the IR for a call to the builtin free function.
596void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
597 createFree(Source, InsertBefore, NULL);
598}
599
600/// CreateFree - Generate the IR for a call to the builtin free function.
601/// Note: This function does not add the call to the basic block, that is the
602/// responsibility of the caller.
603Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
604 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
605 assert(FreeCall && "CreateFree did not create a CallInst");
606 return FreeCall;
607}
608
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609//===----------------------------------------------------------------------===//
610// InvokeInst Implementation
611//===----------------------------------------------------------------------===//
612
613void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000614 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000615 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000616 Use *OL = OperandList;
617 OL[0] = Fn;
618 OL[1] = IfNormal;
619 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000620 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000622 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000623
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000624 assert(((NumArgs == FTy->getNumParams()) ||
625 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000626 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000627
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000628 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000629 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000630 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000631 "Invoking a function with a bad signature!");
632
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000633 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000634 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000635}
636
Misha Brukmanb1c93172005-04-21 23:48:37 +0000637InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000639 OperandTraits<InvokeInst>::op_end(this)
640 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000641 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000642 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000643 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000644 Use *OL = OperandList, *InOL = II.OperandList;
645 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000646 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000647 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000648}
649
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000650BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
651 return getSuccessor(idx);
652}
653unsigned InvokeInst::getNumSuccessorsV() const {
654 return getNumSuccessors();
655}
656void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
657 return setSuccessor(idx, B);
658}
659
Devang Patelba3fa6c2008-09-23 23:03:40 +0000660bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000661 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000662 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000663 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000664 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000665 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000666}
667
Devang Patel4c758ea2008-09-25 21:00:45 +0000668void InvokeInst::addAttribute(unsigned i, Attributes attr) {
669 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000670 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000671 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000672}
673
Devang Patel4c758ea2008-09-25 21:00:45 +0000674void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
675 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000676 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000677 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000678}
679
Duncan Sands5208d1a2007-11-28 17:07:01 +0000680
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000681//===----------------------------------------------------------------------===//
682// ReturnInst Implementation
683//===----------------------------------------------------------------------===//
684
Chris Lattner2195fc42007-02-24 00:55:48 +0000685ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000686 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000687 OperandTraits<ReturnInst>::op_end(this) -
688 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000689 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000690 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000691 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000692 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000693}
694
Owen Anderson55f1c092009-08-13 21:58:54 +0000695ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
696 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000697 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
698 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000699 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000700 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000701}
Owen Anderson55f1c092009-08-13 21:58:54 +0000702ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
703 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000704 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
705 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000706 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000707 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000708}
Owen Anderson55f1c092009-08-13 21:58:54 +0000709ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
710 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000711 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000712}
713
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000714unsigned ReturnInst::getNumSuccessorsV() const {
715 return getNumSuccessors();
716}
717
Devang Patelae682fb2008-02-26 17:56:20 +0000718/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
719/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000721 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722}
723
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000725 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000726 return 0;
727}
728
Devang Patel59643e52008-02-23 00:35:18 +0000729ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000730}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732//===----------------------------------------------------------------------===//
733// UnwindInst Implementation
734//===----------------------------------------------------------------------===//
735
Owen Anderson55f1c092009-08-13 21:58:54 +0000736UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
737 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
738 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000739}
Owen Anderson55f1c092009-08-13 21:58:54 +0000740UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
741 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
742 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000743}
744
745
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000746unsigned UnwindInst::getNumSuccessorsV() const {
747 return getNumSuccessors();
748}
749
750void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000751 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000752}
753
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000754BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000755 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756 return 0;
757}
758
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000759//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000760// UnreachableInst Implementation
761//===----------------------------------------------------------------------===//
762
Owen Anderson55f1c092009-08-13 21:58:54 +0000763UnreachableInst::UnreachableInst(LLVMContext &Context,
764 Instruction *InsertBefore)
765 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
766 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000767}
Owen Anderson55f1c092009-08-13 21:58:54 +0000768UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
769 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
770 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000771}
772
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000773unsigned UnreachableInst::getNumSuccessorsV() const {
774 return getNumSuccessors();
775}
776
777void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000778 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779}
780
781BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000782 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000784}
785
786//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787// BranchInst Implementation
788//===----------------------------------------------------------------------===//
789
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000790void BranchInst::AssertOK() {
791 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000792 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000793 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000794}
795
Chris Lattner2195fc42007-02-24 00:55:48 +0000796BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000797 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000798 OperandTraits<BranchInst>::op_end(this) - 1,
799 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000800 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000801 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000802}
803BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
804 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000805 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000806 OperandTraits<BranchInst>::op_end(this) - 3,
807 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000808 Op<-1>() = IfTrue;
809 Op<-2>() = IfFalse;
810 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000811#ifndef NDEBUG
812 AssertOK();
813#endif
814}
815
816BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000817 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000818 OperandTraits<BranchInst>::op_end(this) - 1,
819 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000820 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000821 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000822}
823
824BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
825 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000826 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000827 OperandTraits<BranchInst>::op_end(this) - 3,
828 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000829 Op<-1>() = IfTrue;
830 Op<-2>() = IfFalse;
831 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000832#ifndef NDEBUG
833 AssertOK();
834#endif
835}
836
837
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000838BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000839 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000840 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
841 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000842 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000843 if (BI.getNumOperands() != 1) {
844 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000845 Op<-3>() = BI.Op<-3>();
846 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000847 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000848 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000849}
850
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000851
852Use* Use::getPrefix() {
853 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
854 if (PotentialPrefix.getOpaqueValue())
855 return 0;
856
857 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
858}
859
860BranchInst::~BranchInst() {
861 if (NumOperands == 1) {
862 if (Use *Prefix = OperandList->getPrefix()) {
863 Op<-1>() = 0;
864 //
865 // mark OperandList to have a special value for scrutiny
866 // by baseclass destructors and operator delete
867 OperandList = Prefix;
868 } else {
869 NumOperands = 3;
870 OperandList = op_begin();
871 }
872 }
873}
874
875
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
877 return getSuccessor(idx);
878}
879unsigned BranchInst::getNumSuccessorsV() const {
880 return getNumSuccessors();
881}
882void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
883 setSuccessor(idx, B);
884}
885
886
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000888// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889//===----------------------------------------------------------------------===//
890
Owen Andersonb6b25302009-07-14 23:09:55 +0000891static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000893 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000894 else {
895 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000896 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000897 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000898 "Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000899 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000900 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901}
902
Victor Hernandez8acf2952009-10-23 21:09:37 +0000903AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
904 const Twine &Name, Instruction *InsertBefore)
905 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
906 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
907 setAlignment(0);
908 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
909 setName(Name);
910}
911
912AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
913 const Twine &Name, BasicBlock *InsertAtEnd)
914 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
915 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
916 setAlignment(0);
917 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
918 setName(Name);
919}
920
921AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
922 Instruction *InsertBefore)
923 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
924 getAISize(Ty->getContext(), 0), InsertBefore) {
925 setAlignment(0);
926 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
927 setName(Name);
928}
929
930AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
931 BasicBlock *InsertAtEnd)
932 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
933 getAISize(Ty->getContext(), 0), InsertAtEnd) {
934 setAlignment(0);
935 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
936 setName(Name);
937}
938
939AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
940 const Twine &Name, Instruction *InsertBefore)
941 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000942 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000943 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000944 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000945 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000946}
947
Victor Hernandez8acf2952009-10-23 21:09:37 +0000948AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
949 const Twine &Name, BasicBlock *InsertAtEnd)
950 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000951 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000952 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000953 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000954 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000955}
956
Gordon Henriksen14a55692007-12-10 02:14:30 +0000957// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000958AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000959}
960
Victor Hernandez8acf2952009-10-23 21:09:37 +0000961void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000962 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
963 SubclassData = Log2_32(Align) + 1;
964 assert(getAlignment() == Align && "Alignment representation error!");
965}
966
Victor Hernandez8acf2952009-10-23 21:09:37 +0000967bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000968 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
969 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000970 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000971}
972
Victor Hernandez8acf2952009-10-23 21:09:37 +0000973const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974 return getType()->getElementType();
975}
976
Chris Lattner8b291e62008-11-26 02:54:17 +0000977/// isStaticAlloca - Return true if this alloca is in the entry block of the
978/// function and is a constant size. If so, the code generator will fold it
979/// into the prolog/epilog code, so it is basically free.
980bool AllocaInst::isStaticAlloca() const {
981 // Must be constant size.
982 if (!isa<ConstantInt>(getArraySize())) return false;
983
984 // Must be in the entry block.
985 const BasicBlock *Parent = getParent();
986 return Parent == &Parent->getParent()->front();
987}
988
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000989//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000990// LoadInst Implementation
991//===----------------------------------------------------------------------===//
992
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000994 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000995 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000996}
997
Daniel Dunbar4975db62009-07-25 04:41:11 +0000998LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000999 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001000 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001001 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001002 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001003 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001004 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001005}
1006
Daniel Dunbar4975db62009-07-25 04:41:11 +00001007LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001008 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001009 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001010 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001011 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001012 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001013 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001014}
1015
Daniel Dunbar4975db62009-07-25 04:41:11 +00001016LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001017 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001018 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001019 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001020 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001021 setAlignment(0);
1022 AssertOK();
1023 setName(Name);
1024}
1025
Daniel Dunbar4975db62009-07-25 04:41:11 +00001026LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001027 unsigned Align, Instruction *InsertBef)
1028 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1029 Load, Ptr, InsertBef) {
1030 setVolatile(isVolatile);
1031 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001032 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001033 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034}
1035
Daniel Dunbar4975db62009-07-25 04:41:11 +00001036LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001037 unsigned Align, BasicBlock *InsertAE)
1038 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1039 Load, Ptr, InsertAE) {
1040 setVolatile(isVolatile);
1041 setAlignment(Align);
1042 AssertOK();
1043 setName(Name);
1044}
1045
Daniel Dunbar4975db62009-07-25 04:41:11 +00001046LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001047 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001048 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001049 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001050 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001051 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +00001052 AssertOK();
1053 setName(Name);
1054}
1055
Daniel Dunbar27096822009-08-11 18:11:15 +00001056
1057
1058LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1059 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1060 Load, Ptr, InsertBef) {
1061 setVolatile(false);
1062 setAlignment(0);
1063 AssertOK();
1064 if (Name && Name[0]) setName(Name);
1065}
1066
1067LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1068 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1069 Load, Ptr, InsertAE) {
1070 setVolatile(false);
1071 setAlignment(0);
1072 AssertOK();
1073 if (Name && Name[0]) setName(Name);
1074}
1075
1076LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1077 Instruction *InsertBef)
1078: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1079 Load, Ptr, InsertBef) {
1080 setVolatile(isVolatile);
1081 setAlignment(0);
1082 AssertOK();
1083 if (Name && Name[0]) setName(Name);
1084}
1085
1086LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1087 BasicBlock *InsertAE)
1088 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1089 Load, Ptr, InsertAE) {
1090 setVolatile(isVolatile);
1091 setAlignment(0);
1092 AssertOK();
1093 if (Name && Name[0]) setName(Name);
1094}
1095
Christopher Lamb84485702007-04-22 19:24:39 +00001096void LoadInst::setAlignment(unsigned Align) {
1097 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1098 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1099}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001100
1101//===----------------------------------------------------------------------===//
1102// StoreInst Implementation
1103//===----------------------------------------------------------------------===//
1104
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001105void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001106 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001107 assert(isa<PointerType>(getOperand(1)->getType()) &&
1108 "Ptr must have pointer type!");
1109 assert(getOperand(0)->getType() ==
1110 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001111 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001112}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001113
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001114
1115StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001116 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001117 OperandTraits<StoreInst>::op_begin(this),
1118 OperandTraits<StoreInst>::operands(this),
1119 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001120 Op<0>() = val;
1121 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001122 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001123 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001124 AssertOK();
1125}
1126
1127StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001128 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001129 OperandTraits<StoreInst>::op_begin(this),
1130 OperandTraits<StoreInst>::operands(this),
1131 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001132 Op<0>() = val;
1133 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001134 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001135 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001136 AssertOK();
1137}
1138
Misha Brukmanb1c93172005-04-21 23:48:37 +00001139StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001140 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001141 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001142 OperandTraits<StoreInst>::op_begin(this),
1143 OperandTraits<StoreInst>::operands(this),
1144 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001145 Op<0>() = val;
1146 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001147 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001148 setAlignment(0);
1149 AssertOK();
1150}
1151
1152StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1153 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001154 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001155 OperandTraits<StoreInst>::op_begin(this),
1156 OperandTraits<StoreInst>::operands(this),
1157 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001158 Op<0>() = val;
1159 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001160 setVolatile(isVolatile);
1161 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001162 AssertOK();
1163}
1164
Misha Brukmanb1c93172005-04-21 23:48:37 +00001165StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001166 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001167 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001168 OperandTraits<StoreInst>::op_begin(this),
1169 OperandTraits<StoreInst>::operands(this),
1170 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001171 Op<0>() = val;
1172 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001173 setVolatile(isVolatile);
1174 setAlignment(Align);
1175 AssertOK();
1176}
1177
1178StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001179 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001180 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001181 OperandTraits<StoreInst>::op_begin(this),
1182 OperandTraits<StoreInst>::operands(this),
1183 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001184 Op<0>() = val;
1185 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001186 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001187 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001188 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001189}
1190
Christopher Lamb84485702007-04-22 19:24:39 +00001191void StoreInst::setAlignment(unsigned Align) {
1192 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1193 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1194}
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) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001394 if (!isa<VectorType>(Val->getType()) ||
1395 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001396 return false;
1397 return true;
1398}
1399
1400
Robert Bocchino23004482006-01-10 19:05:34 +00001401//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001402// InsertElementInst Implementation
1403//===----------------------------------------------------------------------===//
1404
Chris Lattner54865b32006-04-08 04:05:48 +00001405InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001406 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001407 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001408 : Instruction(Vec->getType(), InsertElement,
1409 OperandTraits<InsertElementInst>::op_begin(this),
1410 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001411 assert(isValidOperands(Vec, Elt, Index) &&
1412 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001413 Op<0>() = Vec;
1414 Op<1>() = Elt;
1415 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001416 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001417}
1418
Chris Lattner54865b32006-04-08 04:05:48 +00001419InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001420 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001421 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001422 : Instruction(Vec->getType(), InsertElement,
1423 OperandTraits<InsertElementInst>::op_begin(this),
1424 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001425 assert(isValidOperands(Vec, Elt, Index) &&
1426 "Invalid insertelement instruction operands!");
1427
Gabor Greif2d3024d2008-05-26 21:33:52 +00001428 Op<0>() = Vec;
1429 Op<1>() = Elt;
1430 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001431 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001432}
1433
Chris Lattner54865b32006-04-08 04:05:48 +00001434bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1435 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001436 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001437 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001438
Reid Spencerd84d35b2007-02-15 02:26:10 +00001439 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001440 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001441
Owen Anderson55f1c092009-08-13 21:58:54 +00001442 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001443 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001444 return true;
1445}
1446
1447
Robert Bocchinoca27f032006-01-17 20:07:22 +00001448//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001449// ShuffleVectorInst Implementation
1450//===----------------------------------------------------------------------===//
1451
1452ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001453 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001454 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001455: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001456 cast<VectorType>(Mask->getType())->getNumElements()),
1457 ShuffleVector,
1458 OperandTraits<ShuffleVectorInst>::op_begin(this),
1459 OperandTraits<ShuffleVectorInst>::operands(this),
1460 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001461 assert(isValidOperands(V1, V2, Mask) &&
1462 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001463 Op<0>() = V1;
1464 Op<1>() = V2;
1465 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001466 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001467}
1468
1469ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001470 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001471 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001472: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1473 cast<VectorType>(Mask->getType())->getNumElements()),
1474 ShuffleVector,
1475 OperandTraits<ShuffleVectorInst>::op_begin(this),
1476 OperandTraits<ShuffleVectorInst>::operands(this),
1477 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001478 assert(isValidOperands(V1, V2, Mask) &&
1479 "Invalid shuffle vector instruction operands!");
1480
Gabor Greif2d3024d2008-05-26 21:33:52 +00001481 Op<0>() = V1;
1482 Op<1>() = V2;
1483 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001484 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001485}
1486
Mon P Wang25f01062008-11-10 04:46:22 +00001487bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001488 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001489 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001490 return false;
1491
1492 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1493 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001494 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001495 return false;
1496 return true;
1497}
1498
Chris Lattnerf724e342008-03-02 05:28:33 +00001499/// getMaskValue - Return the index from the shuffle mask for the specified
1500/// output result. This is either -1 if the element is undef or a number less
1501/// than 2*numelements.
1502int ShuffleVectorInst::getMaskValue(unsigned i) const {
1503 const Constant *Mask = cast<Constant>(getOperand(2));
1504 if (isa<UndefValue>(Mask)) return -1;
1505 if (isa<ConstantAggregateZero>(Mask)) return 0;
1506 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1507 assert(i < MaskCV->getNumOperands() && "Index out of range");
1508
1509 if (isa<UndefValue>(MaskCV->getOperand(i)))
1510 return -1;
1511 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1512}
1513
Dan Gohman12fce772008-05-15 19:50:34 +00001514//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001515// InsertValueInst Class
1516//===----------------------------------------------------------------------===//
1517
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001518void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001519 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001520 assert(NumOperands == 2 && "NumOperands not initialized?");
1521 Op<0>() = Agg;
1522 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001523
Dan Gohman1ecaf452008-05-31 00:58:22 +00001524 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001525 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001526}
1527
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001528void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001529 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001530 assert(NumOperands == 2 && "NumOperands not initialized?");
1531 Op<0>() = Agg;
1532 Op<1>() = Val;
1533
1534 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001535 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001536}
1537
1538InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001539 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001540 OperandTraits<InsertValueInst>::op_begin(this), 2),
1541 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001542 Op<0>() = IVI.getOperand(0);
1543 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001544 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001545}
1546
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001547InsertValueInst::InsertValueInst(Value *Agg,
1548 Value *Val,
1549 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001550 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001551 Instruction *InsertBefore)
1552 : Instruction(Agg->getType(), InsertValue,
1553 OperandTraits<InsertValueInst>::op_begin(this),
1554 2, InsertBefore) {
1555 init(Agg, Val, Idx, Name);
1556}
1557
1558InsertValueInst::InsertValueInst(Value *Agg,
1559 Value *Val,
1560 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001561 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001562 BasicBlock *InsertAtEnd)
1563 : Instruction(Agg->getType(), InsertValue,
1564 OperandTraits<InsertValueInst>::op_begin(this),
1565 2, InsertAtEnd) {
1566 init(Agg, Val, Idx, Name);
1567}
1568
Dan Gohman0752bff2008-05-23 00:36:11 +00001569//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001570// ExtractValueInst Class
1571//===----------------------------------------------------------------------===//
1572
Gabor Greifcbcc4952008-06-06 21:06:32 +00001573void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001574 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001575 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001576
Dan Gohman1ecaf452008-05-31 00:58:22 +00001577 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001578 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001579}
1580
Daniel Dunbar4975db62009-07-25 04:41:11 +00001581void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001582 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001583
1584 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001585 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001586}
1587
1588ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001589 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001590 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001591 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001592}
1593
Dan Gohman12fce772008-05-15 19:50:34 +00001594// getIndexedType - Returns the type of the element that would be extracted
1595// with an extractvalue instruction with the specified parameters.
1596//
1597// A null type is returned if the indices are invalid for the specified
1598// pointer type.
1599//
1600const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001601 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001602 unsigned NumIdx) {
1603 unsigned CurIdx = 0;
1604 for (; CurIdx != NumIdx; ++CurIdx) {
1605 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001606 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1607 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001608 if (!CT->indexValid(Index)) return 0;
1609 Agg = CT->getTypeAtIndex(Index);
1610
1611 // If the new type forwards to another type, then it is in the middle
1612 // of being refined to another type (and hence, may have dropped all
1613 // references to what it was using before). So, use the new forwarded
1614 // type.
1615 if (const Type *Ty = Agg->getForwardedType())
1616 Agg = Ty;
1617 }
1618 return CurIdx == NumIdx ? Agg : 0;
1619}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001620
Dan Gohman4a3125b2008-06-17 21:07:55 +00001621const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001622 unsigned Idx) {
1623 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001624}
1625
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001626//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001627// BinaryOperator Class
1628//===----------------------------------------------------------------------===//
1629
Dan Gohmana5b96452009-06-04 22:49:04 +00001630/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1631/// type is floating-point, to help provide compatibility with an older API.
1632///
1633static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1634 const Type *Ty) {
1635 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1636 if (Ty->isFPOrFPVector()) {
1637 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1638 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1639 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1640 }
1641 return iType;
1642}
1643
Chris Lattner2195fc42007-02-24 00:55:48 +00001644BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001645 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001646 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001647 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001648 OperandTraits<BinaryOperator>::op_begin(this),
1649 OperandTraits<BinaryOperator>::operands(this),
1650 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001651 Op<0>() = S1;
1652 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001653 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001654 setName(Name);
1655}
1656
1657BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001658 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001659 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001660 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001661 OperandTraits<BinaryOperator>::op_begin(this),
1662 OperandTraits<BinaryOperator>::operands(this),
1663 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001664 Op<0>() = S1;
1665 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001666 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001667 setName(Name);
1668}
1669
1670
1671void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001672 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001673 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001674 assert(LHS->getType() == RHS->getType() &&
1675 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676#ifndef NDEBUG
1677 switch (iType) {
1678 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001679 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001680 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001682 assert(getType()->isIntOrIntVector() &&
1683 "Tried to create an integer operation on a non-integer type!");
1684 break;
1685 case FAdd: case FSub:
1686 case FMul:
1687 assert(getType() == LHS->getType() &&
1688 "Arithmetic operation should return same type as operands!");
1689 assert(getType()->isFPOrFPVector() &&
1690 "Tried to create a floating-point operation on a "
1691 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001692 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001693 case UDiv:
1694 case SDiv:
1695 assert(getType() == LHS->getType() &&
1696 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001697 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1698 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001699 "Incorrect operand type (not integer) for S/UDIV");
1700 break;
1701 case FDiv:
1702 assert(getType() == LHS->getType() &&
1703 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001704 assert(getType()->isFPOrFPVector() &&
1705 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001706 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001707 case URem:
1708 case SRem:
1709 assert(getType() == LHS->getType() &&
1710 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001711 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1712 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001713 "Incorrect operand type (not integer) for S/UREM");
1714 break;
1715 case FRem:
1716 assert(getType() == LHS->getType() &&
1717 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001718 assert(getType()->isFPOrFPVector() &&
1719 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001720 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001721 case Shl:
1722 case LShr:
1723 case AShr:
1724 assert(getType() == LHS->getType() &&
1725 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001726 assert((getType()->isInteger() ||
1727 (isa<VectorType>(getType()) &&
1728 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1729 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001730 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001731 case And: case Or:
1732 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001733 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001734 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001735 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001736 (isa<VectorType>(getType()) &&
1737 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001738 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001739 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001740 default:
1741 break;
1742 }
1743#endif
1744}
1745
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001746BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001747 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001748 Instruction *InsertBefore) {
1749 assert(S1->getType() == S2->getType() &&
1750 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001751 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001752}
1753
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001754BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001755 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001756 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001757 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001758 InsertAtEnd->getInstList().push_back(Res);
1759 return Res;
1760}
1761
Dan Gohman5476cfd2009-08-12 16:23:25 +00001762BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001763 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001764 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001765 return new BinaryOperator(Instruction::Sub,
1766 zero, Op,
1767 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001768}
1769
Dan Gohman5476cfd2009-08-12 16:23:25 +00001770BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001771 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001772 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001773 return new BinaryOperator(Instruction::Sub,
1774 zero, Op,
1775 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001776}
1777
Dan Gohman5476cfd2009-08-12 16:23:25 +00001778BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001779 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001780 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001781 return new BinaryOperator(Instruction::FSub,
1782 zero, Op,
1783 Op->getType(), Name, InsertBefore);
1784}
1785
Dan Gohman5476cfd2009-08-12 16:23:25 +00001786BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001787 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001788 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001789 return new BinaryOperator(Instruction::FSub,
1790 zero, Op,
1791 Op->getType(), Name, InsertAtEnd);
1792}
1793
Dan Gohman5476cfd2009-08-12 16:23:25 +00001794BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001795 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001796 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001797 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001798 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001799 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001800 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001801 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001802 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001803 }
1804
1805 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001806 Op->getType(), Name, InsertBefore);
1807}
1808
Dan Gohman5476cfd2009-08-12 16:23:25 +00001809BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001810 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001811 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001812 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001813 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001814 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001815 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001816 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001817 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001818 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001819 }
1820
1821 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001822 Op->getType(), Name, InsertAtEnd);
1823}
1824
1825
1826// isConstantAllOnes - Helper function for several functions below
1827static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001828 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1829 return CI->isAllOnesValue();
1830 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1831 return CV->isAllOnesValue();
1832 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001833}
1834
Owen Andersonbb2501b2009-07-13 22:18:28 +00001835bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001836 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1837 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001838 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1839 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001840 return false;
1841}
1842
Owen Andersonbb2501b2009-07-13 22:18:28 +00001843bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001844 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1845 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001846 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001847 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001848 return false;
1849}
1850
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001851bool BinaryOperator::isNot(const Value *V) {
1852 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1853 return (Bop->getOpcode() == Instruction::Xor &&
1854 (isConstantAllOnes(Bop->getOperand(1)) ||
1855 isConstantAllOnes(Bop->getOperand(0))));
1856 return false;
1857}
1858
Chris Lattner2c7d1772005-04-24 07:28:37 +00001859Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001860 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001861}
1862
Chris Lattner2c7d1772005-04-24 07:28:37 +00001863const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1864 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001865}
1866
Dan Gohmana5b96452009-06-04 22:49:04 +00001867Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001868 return cast<BinaryOperator>(BinOp)->getOperand(1);
1869}
1870
1871const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1872 return getFNegArgument(const_cast<Value*>(BinOp));
1873}
1874
Chris Lattner2c7d1772005-04-24 07:28:37 +00001875Value *BinaryOperator::getNotArgument(Value *BinOp) {
1876 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1877 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1878 Value *Op0 = BO->getOperand(0);
1879 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001880 if (isConstantAllOnes(Op0)) return Op1;
1881
1882 assert(isConstantAllOnes(Op1));
1883 return Op0;
1884}
1885
Chris Lattner2c7d1772005-04-24 07:28:37 +00001886const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1887 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001888}
1889
1890
1891// swapOperands - Exchange the two operands to this instruction. This
1892// instruction is safe to use on any binary instruction and does not
1893// modify the semantics of the instruction. If the instruction is
1894// order dependent (SetLT f.e.) the opcode is changed.
1895//
1896bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001897 if (!isCommutative())
1898 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001899 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001900 return false;
1901}
1902
Dan Gohman1b849082009-09-07 23:54:19 +00001903void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1904 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1905}
1906
1907void BinaryOperator::setHasNoSignedWrap(bool b) {
1908 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1909}
1910
1911void BinaryOperator::setIsExact(bool b) {
1912 cast<SDivOperator>(this)->setIsExact(b);
1913}
1914
Nick Lewycky28a5f252009-09-27 21:33:04 +00001915bool BinaryOperator::hasNoUnsignedWrap() const {
1916 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1917}
1918
1919bool BinaryOperator::hasNoSignedWrap() const {
1920 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1921}
1922
1923bool BinaryOperator::isExact() const {
1924 return cast<SDivOperator>(this)->isExact();
1925}
1926
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001927//===----------------------------------------------------------------------===//
1928// CastInst Class
1929//===----------------------------------------------------------------------===//
1930
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001931// Just determine if this cast only deals with integral->integral conversion.
1932bool CastInst::isIntegerCast() const {
1933 switch (getOpcode()) {
1934 default: return false;
1935 case Instruction::ZExt:
1936 case Instruction::SExt:
1937 case Instruction::Trunc:
1938 return true;
1939 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001940 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001941 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001942}
1943
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001944bool CastInst::isLosslessCast() const {
1945 // Only BitCast can be lossless, exit fast if we're not BitCast
1946 if (getOpcode() != Instruction::BitCast)
1947 return false;
1948
1949 // Identity cast is always lossless
1950 const Type* SrcTy = getOperand(0)->getType();
1951 const Type* DstTy = getType();
1952 if (SrcTy == DstTy)
1953 return true;
1954
Reid Spencer8d9336d2006-12-31 05:26:44 +00001955 // Pointer to pointer is always lossless.
1956 if (isa<PointerType>(SrcTy))
1957 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001958 return false; // Other types have no identity values
1959}
1960
1961/// This function determines if the CastInst does not require any bits to be
1962/// changed in order to effect the cast. Essentially, it identifies cases where
1963/// no code gen is necessary for the cast, hence the name no-op cast. For
1964/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001965/// # bitcast i32* %x to i8*
1966/// # bitcast <2 x i32> %x to <4 x i16>
1967/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001968/// @brief Determine if a cast is a no-op.
1969bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1970 switch (getOpcode()) {
1971 default:
1972 assert(!"Invalid CastOp");
1973 case Instruction::Trunc:
1974 case Instruction::ZExt:
1975 case Instruction::SExt:
1976 case Instruction::FPTrunc:
1977 case Instruction::FPExt:
1978 case Instruction::UIToFP:
1979 case Instruction::SIToFP:
1980 case Instruction::FPToUI:
1981 case Instruction::FPToSI:
1982 return false; // These always modify bits
1983 case Instruction::BitCast:
1984 return true; // BitCast never modifies bits.
1985 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001986 return IntPtrTy->getScalarSizeInBits() ==
1987 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001989 return IntPtrTy->getScalarSizeInBits() ==
1990 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001991 }
1992}
1993
1994/// This function determines if a pair of casts can be eliminated and what
1995/// opcode should be used in the elimination. This assumes that there are two
1996/// instructions like this:
1997/// * %F = firstOpcode SrcTy %x to MidTy
1998/// * %S = secondOpcode MidTy %F to DstTy
1999/// The function returns a resultOpcode so these two casts can be replaced with:
2000/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2001/// If no such cast is permited, the function returns 0.
2002unsigned CastInst::isEliminableCastPair(
2003 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
2004 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
2005{
2006 // Define the 144 possibilities for these two cast instructions. The values
2007 // in this matrix determine what to do in a given situation and select the
2008 // case in the switch below. The rows correspond to firstOp, the columns
2009 // correspond to secondOp. In looking at the table below, keep in mind
2010 // the following cast properties:
2011 //
2012 // Size Compare Source Destination
2013 // Operator Src ? Size Type Sign Type Sign
2014 // -------- ------------ ------------------- ---------------------
2015 // TRUNC > Integer Any Integral Any
2016 // ZEXT < Integral Unsigned Integer Any
2017 // SEXT < Integral Signed Integer Any
2018 // FPTOUI n/a FloatPt n/a Integral Unsigned
2019 // FPTOSI n/a FloatPt n/a Integral Signed
2020 // UITOFP n/a Integral Unsigned FloatPt n/a
2021 // SITOFP n/a Integral Signed FloatPt n/a
2022 // FPTRUNC > FloatPt n/a FloatPt n/a
2023 // FPEXT < FloatPt n/a FloatPt n/a
2024 // PTRTOINT n/a Pointer n/a Integral Unsigned
2025 // INTTOPTR n/a Integral Unsigned Pointer n/a
2026 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002027 //
2028 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002029 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2030 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002031 // of the produced value (we no longer know the top-part is all zeros).
2032 // Further this conversion is often much more expensive for typical hardware,
2033 // and causes issues when building libgcc. We disallow fptosi+sext for the
2034 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002035 const unsigned numCastOps =
2036 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2037 static const uint8_t CastResults[numCastOps][numCastOps] = {
2038 // T F F U S F F P I B -+
2039 // R Z S P P I I T P 2 N T |
2040 // U E E 2 2 2 2 R E I T C +- secondOp
2041 // N X X U S F F N X N 2 V |
2042 // C T T I I P P C T T P T -+
2043 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2044 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2045 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002046 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2047 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002048 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2049 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2050 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2051 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2052 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2053 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2054 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2055 };
2056
2057 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2058 [secondOp-Instruction::CastOpsBegin];
2059 switch (ElimCase) {
2060 case 0:
2061 // categorically disallowed
2062 return 0;
2063 case 1:
2064 // allowed, use first cast's opcode
2065 return firstOp;
2066 case 2:
2067 // allowed, use second cast's opcode
2068 return secondOp;
2069 case 3:
2070 // no-op cast in second op implies firstOp as long as the DestTy
2071 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00002072 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002073 return firstOp;
2074 return 0;
2075 case 4:
2076 // no-op cast in second op implies firstOp as long as the DestTy
2077 // is floating point
2078 if (DstTy->isFloatingPoint())
2079 return firstOp;
2080 return 0;
2081 case 5:
2082 // no-op cast in first op implies secondOp as long as the SrcTy
2083 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00002084 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002085 return secondOp;
2086 return 0;
2087 case 6:
2088 // no-op cast in first op implies secondOp as long as the SrcTy
2089 // is a floating point
2090 if (SrcTy->isFloatingPoint())
2091 return secondOp;
2092 return 0;
2093 case 7: {
2094 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002095 if (!IntPtrTy)
2096 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002097 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2098 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002099 if (MidSize >= PtrSize)
2100 return Instruction::BitCast;
2101 return 0;
2102 }
2103 case 8: {
2104 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2105 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2106 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002107 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2108 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002109 if (SrcSize == DstSize)
2110 return Instruction::BitCast;
2111 else if (SrcSize < DstSize)
2112 return firstOp;
2113 return secondOp;
2114 }
2115 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2116 return Instruction::ZExt;
2117 case 10:
2118 // fpext followed by ftrunc is allowed if the bit size returned to is
2119 // the same as the original, in which case its just a bitcast
2120 if (SrcTy == DstTy)
2121 return Instruction::BitCast;
2122 return 0; // If the types are not the same we can't eliminate it.
2123 case 11:
2124 // bitcast followed by ptrtoint is allowed as long as the bitcast
2125 // is a pointer to pointer cast.
2126 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2127 return secondOp;
2128 return 0;
2129 case 12:
2130 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2131 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2132 return firstOp;
2133 return 0;
2134 case 13: {
2135 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002136 if (!IntPtrTy)
2137 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002138 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2139 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2140 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002141 if (SrcSize <= PtrSize && SrcSize == DstSize)
2142 return Instruction::BitCast;
2143 return 0;
2144 }
2145 case 99:
2146 // cast combination can't happen (error in input). This is for all cases
2147 // where the MidTy is not the same for the two cast instructions.
2148 assert(!"Invalid Cast Combination");
2149 return 0;
2150 default:
2151 assert(!"Error in CastResults table!!!");
2152 return 0;
2153 }
2154 return 0;
2155}
2156
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002157CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002158 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002159 // Construct and return the appropriate CastInst subclass
2160 switch (op) {
2161 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2162 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2163 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2164 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2165 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2166 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2167 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2168 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2169 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2170 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2171 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2172 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2173 default:
2174 assert(!"Invalid opcode provided");
2175 }
2176 return 0;
2177}
2178
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002179CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002180 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181 // Construct and return the appropriate CastInst subclass
2182 switch (op) {
2183 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2184 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2185 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2186 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2187 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2188 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2189 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2190 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2191 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2192 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2193 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2194 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2195 default:
2196 assert(!"Invalid opcode provided");
2197 }
2198 return 0;
2199}
2200
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002201CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002202 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002203 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002204 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002205 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2206 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002207}
2208
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002209CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002210 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002211 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002212 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002213 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2214 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002215}
2216
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002217CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002218 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002219 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002220 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002221 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2222 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002223}
2224
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002225CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002226 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002227 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002228 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002229 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2230 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002231}
2232
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002233CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002234 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002235 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002236 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002237 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2238 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002239}
2240
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002241CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002242 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002243 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002244 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002245 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2246 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002247}
2248
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002249CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002250 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002251 BasicBlock *InsertAtEnd) {
2252 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002253 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002254 "Invalid cast");
2255
Chris Lattner03c49532007-01-15 02:27:26 +00002256 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002257 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2258 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002259}
2260
2261/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002262CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002263 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002264 Instruction *InsertBefore) {
2265 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002266 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002267 "Invalid cast");
2268
Chris Lattner03c49532007-01-15 02:27:26 +00002269 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002270 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2271 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002272}
2273
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002274CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002275 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002276 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002277 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002278 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2279 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002280 Instruction::CastOps opcode =
2281 (SrcBits == DstBits ? Instruction::BitCast :
2282 (SrcBits > DstBits ? Instruction::Trunc :
2283 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002284 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002285}
2286
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002287CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002288 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002289 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002290 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2291 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002292 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2293 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002294 Instruction::CastOps opcode =
2295 (SrcBits == DstBits ? Instruction::BitCast :
2296 (SrcBits > DstBits ? Instruction::Trunc :
2297 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002298 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002299}
2300
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002301CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002302 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002303 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002304 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002305 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002306 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2307 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002308 Instruction::CastOps opcode =
2309 (SrcBits == DstBits ? Instruction::BitCast :
2310 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002311 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002312}
2313
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002314CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002315 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002316 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002317 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002318 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002319 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2320 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002321 Instruction::CastOps opcode =
2322 (SrcBits == DstBits ? Instruction::BitCast :
2323 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002324 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002325}
2326
Duncan Sands55e50902008-01-06 10:12:28 +00002327// Check whether it is valid to call getCastOpcode for these types.
2328// This routine must be kept in sync with getCastOpcode.
2329bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2330 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2331 return false;
2332
2333 if (SrcTy == DestTy)
2334 return true;
2335
2336 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002337 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2338 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002339
2340 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002341 if (DestTy->isInteger()) { // Casting to integral
2342 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002343 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002344 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002345 return true;
2346 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002347 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002348 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002349 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002350 return isa<PointerType>(SrcTy);
2351 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002352 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
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 false;
2362 }
2363 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002364 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002365 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002366 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002367 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002368 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002369 return DestPTy->getBitWidth() == SrcBits;
2370 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002371 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2372 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002373 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002374 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002375 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002376 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002377 return false;
2378 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002379 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002380 return false;
2381 }
2382}
2383
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384// Provide a way to get a "cast" where the cast opcode is inferred from the
2385// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002386// logic in the castIsValid function below. This axiom should hold:
2387// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2388// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002390// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002392CastInst::getCastOpcode(
2393 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002394 // Get the bit sizes, we'll need these
2395 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002396 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2397 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398
Duncan Sands55e50902008-01-06 10:12:28 +00002399 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2400 "Only first class types are castable!");
2401
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002403 if (DestTy->isInteger()) { // Casting to integral
2404 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405 if (DestBits < SrcBits)
2406 return Trunc; // int -> smaller int
2407 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002408 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409 return SExt; // signed -> SEXT
2410 else
2411 return ZExt; // unsigned -> ZEXT
2412 } else {
2413 return BitCast; // Same size, No-op cast
2414 }
2415 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002416 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417 return FPToSI; // FP -> sint
2418 else
2419 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002420 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002422 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002423 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424 return BitCast; // Same size, no-op cast
2425 } else {
2426 assert(isa<PointerType>(SrcTy) &&
2427 "Casting from a value that is not first-class type");
2428 return PtrToInt; // ptr -> int
2429 }
2430 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002431 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002432 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433 return SIToFP; // sint -> FP
2434 else
2435 return UIToFP; // uint -> FP
2436 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2437 if (DestBits < SrcBits) {
2438 return FPTrunc; // FP -> smaller FP
2439 } else if (DestBits > SrcBits) {
2440 return FPExt; // FP -> larger FP
2441 } else {
2442 return BitCast; // same size, no-op cast
2443 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002444 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002446 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002447 PTy = NULL;
2448 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002450 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002452 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2453 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002455 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002456 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002457 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002459 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002461 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462 }
2463 } else if (isa<PointerType>(DestTy)) {
2464 if (isa<PointerType>(SrcTy)) {
2465 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002466 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467 return IntToPtr; // int -> ptr
2468 } else {
2469 assert(!"Casting pointer to other than pointer or int");
2470 }
2471 } else {
2472 assert(!"Casting to type that is not first-class");
2473 }
2474
2475 // If we fall through to here we probably hit an assertion cast above
2476 // and assertions are not turned on. Anything we return is an error, so
2477 // BitCast is as good a choice as any.
2478 return BitCast;
2479}
2480
2481//===----------------------------------------------------------------------===//
2482// CastInst SubClass Constructors
2483//===----------------------------------------------------------------------===//
2484
2485/// Check that the construction parameters for a CastInst are correct. This
2486/// could be broken out into the separate constructors but it is useful to have
2487/// it in one place and to eliminate the redundant code for getting the sizes
2488/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002489bool
2490CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491
2492 // Check for type sanity on the arguments
2493 const Type *SrcTy = S->getType();
2494 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2495 return false;
2496
2497 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002498 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2499 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500
2501 // Switch on the opcode provided
2502 switch (op) {
2503 default: return false; // This is an input error
2504 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002505 return SrcTy->isIntOrIntVector() &&
2506 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002508 return SrcTy->isIntOrIntVector() &&
2509 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002511 return SrcTy->isIntOrIntVector() &&
2512 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002514 return SrcTy->isFPOrFPVector() &&
2515 DstTy->isFPOrFPVector() &&
2516 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002518 return SrcTy->isFPOrFPVector() &&
2519 DstTy->isFPOrFPVector() &&
2520 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002523 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2524 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002525 return SVTy->getElementType()->isIntOrIntVector() &&
2526 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002527 SVTy->getNumElements() == DVTy->getNumElements();
2528 }
2529 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002530 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002533 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2534 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002535 return SVTy->getElementType()->isFPOrFPVector() &&
2536 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002537 SVTy->getNumElements() == DVTy->getNumElements();
2538 }
2539 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002540 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002542 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002544 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545 case Instruction::BitCast:
2546 // BitCast implies a no-op cast of type only. No bits change.
2547 // However, you can't cast pointers to anything but pointers.
2548 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2549 return false;
2550
Duncan Sands55e50902008-01-06 10:12:28 +00002551 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552 // these cases, the cast is okay if the source and destination bit widths
2553 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002554 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555 }
2556}
2557
2558TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002559 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002561 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562}
2563
2564TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002565 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002567 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568}
2569
2570ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002571 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002573 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574}
2575
2576ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002577 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002578) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002579 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580}
2581SExtInst::SExtInst(
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, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002584 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585}
2586
Jeff Cohencc08c832006-12-02 02:22:01 +00002587SExtInst::SExtInst(
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, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002590 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591}
2592
2593FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002594 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002596 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597}
2598
2599FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002600 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002602 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603}
2604
2605FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002606 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002608 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609}
2610
2611FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002612 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002614 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615}
2616
2617UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002618 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002620 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621}
2622
2623UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002624 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002626 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002627}
2628
2629SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002630 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002632 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633}
2634
2635SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002636 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002638 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002639}
2640
2641FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002642 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002644 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645}
2646
2647FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002648 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002649) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002650 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002651}
2652
2653FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002654 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002656 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657}
2658
2659FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002660 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002662 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002663}
2664
2665PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002666 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002667) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002668 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002669}
2670
2671PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002672 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002673) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002674 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002675}
2676
2677IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002678 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002680 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681}
2682
2683IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002684 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002685) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002686 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687}
2688
2689BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002690 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002691) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002692 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002693}
2694
2695BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002696 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002697) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002698 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002699}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002700
2701//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002702// CmpInst Classes
2703//===----------------------------------------------------------------------===//
2704
Nate Begemand2195702008-05-12 19:01:56 +00002705CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002706 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002707 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002708 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002709 OperandTraits<CmpInst>::op_begin(this),
2710 OperandTraits<CmpInst>::operands(this),
2711 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002712 Op<0>() = LHS;
2713 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002714 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002715 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002716}
Gabor Greiff6caff662008-05-10 08:32:32 +00002717
Nate Begemand2195702008-05-12 19:01:56 +00002718CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002719 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002720 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002721 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002722 OperandTraits<CmpInst>::op_begin(this),
2723 OperandTraits<CmpInst>::operands(this),
2724 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002725 Op<0>() = LHS;
2726 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002727 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002728 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002729}
2730
2731CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002732CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002733 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002734 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002735 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002736 if (InsertBefore)
2737 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2738 S1, S2, Name);
2739 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002740 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002741 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002742 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002743
2744 if (InsertBefore)
2745 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2746 S1, S2, Name);
2747 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002748 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002749 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002750}
2751
2752CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002753CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002754 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002755 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002756 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2757 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002758 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002759 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2760 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002761}
2762
2763void CmpInst::swapOperands() {
2764 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2765 IC->swapOperands();
2766 else
2767 cast<FCmpInst>(this)->swapOperands();
2768}
2769
2770bool CmpInst::isCommutative() {
2771 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2772 return IC->isCommutative();
2773 return cast<FCmpInst>(this)->isCommutative();
2774}
2775
2776bool CmpInst::isEquality() {
2777 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2778 return IC->isEquality();
2779 return cast<FCmpInst>(this)->isEquality();
2780}
2781
2782
Dan Gohman4e724382008-05-31 02:47:54 +00002783CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002784 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002785 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002786 case ICMP_EQ: return ICMP_NE;
2787 case ICMP_NE: return ICMP_EQ;
2788 case ICMP_UGT: return ICMP_ULE;
2789 case ICMP_ULT: return ICMP_UGE;
2790 case ICMP_UGE: return ICMP_ULT;
2791 case ICMP_ULE: return ICMP_UGT;
2792 case ICMP_SGT: return ICMP_SLE;
2793 case ICMP_SLT: return ICMP_SGE;
2794 case ICMP_SGE: return ICMP_SLT;
2795 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002796
Dan Gohman4e724382008-05-31 02:47:54 +00002797 case FCMP_OEQ: return FCMP_UNE;
2798 case FCMP_ONE: return FCMP_UEQ;
2799 case FCMP_OGT: return FCMP_ULE;
2800 case FCMP_OLT: return FCMP_UGE;
2801 case FCMP_OGE: return FCMP_ULT;
2802 case FCMP_OLE: return FCMP_UGT;
2803 case FCMP_UEQ: return FCMP_ONE;
2804 case FCMP_UNE: return FCMP_OEQ;
2805 case FCMP_UGT: return FCMP_OLE;
2806 case FCMP_ULT: return FCMP_OGE;
2807 case FCMP_UGE: return FCMP_OLT;
2808 case FCMP_ULE: return FCMP_OGT;
2809 case FCMP_ORD: return FCMP_UNO;
2810 case FCMP_UNO: return FCMP_ORD;
2811 case FCMP_TRUE: return FCMP_FALSE;
2812 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002813 }
2814}
2815
Reid Spencer266e42b2006-12-23 06:05:41 +00002816ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2817 switch (pred) {
2818 default: assert(! "Unknown icmp predicate!");
2819 case ICMP_EQ: case ICMP_NE:
2820 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2821 return pred;
2822 case ICMP_UGT: return ICMP_SGT;
2823 case ICMP_ULT: return ICMP_SLT;
2824 case ICMP_UGE: return ICMP_SGE;
2825 case ICMP_ULE: return ICMP_SLE;
2826 }
2827}
2828
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002829ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2830 switch (pred) {
2831 default: assert(! "Unknown icmp predicate!");
2832 case ICMP_EQ: case ICMP_NE:
2833 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2834 return pred;
2835 case ICMP_SGT: return ICMP_UGT;
2836 case ICMP_SLT: return ICMP_ULT;
2837 case ICMP_SGE: return ICMP_UGE;
2838 case ICMP_SLE: return ICMP_ULE;
2839 }
2840}
2841
Reid Spencer0286bc12007-02-28 22:00:54 +00002842/// Initialize a set of values that all satisfy the condition with C.
2843///
2844ConstantRange
2845ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2846 APInt Lower(C);
2847 APInt Upper(C);
2848 uint32_t BitWidth = C.getBitWidth();
2849 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002850 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002851 case ICmpInst::ICMP_EQ: Upper++; break;
2852 case ICmpInst::ICMP_NE: Lower++; break;
2853 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2854 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2855 case ICmpInst::ICMP_UGT:
2856 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2857 break;
2858 case ICmpInst::ICMP_SGT:
2859 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2860 break;
2861 case ICmpInst::ICMP_ULE:
2862 Lower = APInt::getMinValue(BitWidth); Upper++;
2863 break;
2864 case ICmpInst::ICMP_SLE:
2865 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2866 break;
2867 case ICmpInst::ICMP_UGE:
2868 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2869 break;
2870 case ICmpInst::ICMP_SGE:
2871 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2872 break;
2873 }
2874 return ConstantRange(Lower, Upper);
2875}
2876
Dan Gohman4e724382008-05-31 02:47:54 +00002877CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002878 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002879 default: assert(!"Unknown cmp predicate!");
2880 case ICMP_EQ: case ICMP_NE:
2881 return pred;
2882 case ICMP_SGT: return ICMP_SLT;
2883 case ICMP_SLT: return ICMP_SGT;
2884 case ICMP_SGE: return ICMP_SLE;
2885 case ICMP_SLE: return ICMP_SGE;
2886 case ICMP_UGT: return ICMP_ULT;
2887 case ICMP_ULT: return ICMP_UGT;
2888 case ICMP_UGE: return ICMP_ULE;
2889 case ICMP_ULE: return ICMP_UGE;
2890
Reid Spencerd9436b62006-11-20 01:22:35 +00002891 case FCMP_FALSE: case FCMP_TRUE:
2892 case FCMP_OEQ: case FCMP_ONE:
2893 case FCMP_UEQ: case FCMP_UNE:
2894 case FCMP_ORD: case FCMP_UNO:
2895 return pred;
2896 case FCMP_OGT: return FCMP_OLT;
2897 case FCMP_OLT: return FCMP_OGT;
2898 case FCMP_OGE: return FCMP_OLE;
2899 case FCMP_OLE: return FCMP_OGE;
2900 case FCMP_UGT: return FCMP_ULT;
2901 case FCMP_ULT: return FCMP_UGT;
2902 case FCMP_UGE: return FCMP_ULE;
2903 case FCMP_ULE: return FCMP_UGE;
2904 }
2905}
2906
Reid Spencer266e42b2006-12-23 06:05:41 +00002907bool CmpInst::isUnsigned(unsigned short predicate) {
2908 switch (predicate) {
2909 default: return false;
2910 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2911 case ICmpInst::ICMP_UGE: return true;
2912 }
2913}
2914
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002915bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002916 switch (predicate) {
2917 default: return false;
2918 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2919 case ICmpInst::ICMP_SGE: return true;
2920 }
2921}
2922
2923bool CmpInst::isOrdered(unsigned short predicate) {
2924 switch (predicate) {
2925 default: return false;
2926 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2927 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2928 case FCmpInst::FCMP_ORD: return true;
2929 }
2930}
2931
2932bool CmpInst::isUnordered(unsigned short predicate) {
2933 switch (predicate) {
2934 default: return false;
2935 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2936 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2937 case FCmpInst::FCMP_UNO: return true;
2938 }
2939}
2940
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002941bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2942 switch(predicate) {
2943 default: return false;
2944 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2945 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2946 }
2947}
2948
2949bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2950 switch(predicate) {
2951 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2952 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2953 default: return false;
2954 }
2955}
2956
2957
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002958//===----------------------------------------------------------------------===//
2959// SwitchInst Implementation
2960//===----------------------------------------------------------------------===//
2961
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002962void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002963 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002964 ReservedSpace = 2+NumCases*2;
2965 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002966 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002967
Gabor Greif2d3024d2008-05-26 21:33:52 +00002968 OperandList[0] = Value;
2969 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002970}
2971
Chris Lattner2195fc42007-02-24 00:55:48 +00002972/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2973/// switch on and a default destination. The number of additional cases can
2974/// be specified here to make memory allocation more efficient. This
2975/// constructor can also autoinsert before another instruction.
2976SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2977 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002978 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2979 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002980 init(Value, Default, NumCases);
2981}
2982
2983/// 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 also autoinserts at the end of the specified BasicBlock.
2987SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2988 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002989 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2990 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002991 init(Value, Default, NumCases);
2992}
2993
Misha Brukmanb1c93172005-04-21 23:48:37 +00002994SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002995 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002996 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002997 Use *OL = OperandList, *InOL = SI.OperandList;
2998 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002999 OL[i] = InOL[i];
3000 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003001 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003002 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003003}
3004
Gordon Henriksen14a55692007-12-10 02:14:30 +00003005SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003006 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003007}
3008
3009
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003010/// addCase - Add an entry to the switch instruction...
3011///
Chris Lattner47ac1872005-02-24 05:32:09 +00003012void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003013 unsigned OpNo = NumOperands;
3014 if (OpNo+2 > ReservedSpace)
3015 resizeOperands(0); // Get more space!
3016 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003017 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003018 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003019 OperandList[OpNo] = OnVal;
3020 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003021}
3022
3023/// removeCase - This method removes the specified successor from the switch
3024/// instruction. Note that this cannot be used to remove the default
3025/// destination (successor #0).
3026///
3027void SwitchInst::removeCase(unsigned idx) {
3028 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003029 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3030
3031 unsigned NumOps = getNumOperands();
3032 Use *OL = OperandList;
3033
3034 // Move everything after this operand down.
3035 //
3036 // FIXME: we could just swap with the end of the list, then erase. However,
3037 // client might not expect this to happen. The code as it is thrashes the
3038 // use/def lists, which is kinda lame.
3039 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3040 OL[i-2] = OL[i];
3041 OL[i-2+1] = OL[i+1];
3042 }
3043
3044 // Nuke the last value.
3045 OL[NumOps-2].set(0);
3046 OL[NumOps-2+1].set(0);
3047 NumOperands = NumOps-2;
3048}
3049
3050/// resizeOperands - resize operands - This adjusts the length of the operands
3051/// list according to the following behavior:
3052/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003053/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003054/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3055/// 3. If NumOps == NumOperands, trim the reserved space.
3056///
3057void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003058 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003059 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003060 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003061 } else if (NumOps*2 > NumOperands) {
3062 // No resize needed.
3063 if (ReservedSpace >= NumOps) return;
3064 } else if (NumOps == NumOperands) {
3065 if (ReservedSpace == NumOps) return;
3066 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003067 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003068 }
3069
3070 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003071 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003072 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003073 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003074 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003075 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003076 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003077 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003078}
3079
3080
3081BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3082 return getSuccessor(idx);
3083}
3084unsigned SwitchInst::getNumSuccessorsV() const {
3085 return getNumSuccessors();
3086}
3087void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3088 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003089}
Chris Lattnerf22be932004-10-15 23:52:53 +00003090
Chris Lattner3ed871f2009-10-27 19:13:16 +00003091//===----------------------------------------------------------------------===//
3092// SwitchInst Implementation
3093//===----------------------------------------------------------------------===//
3094
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003095void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Chris Lattner6747b4c2009-10-29 05:53:32 +00003096 assert(Address && isa<PointerType>(Address->getType()) &&
3097 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003098 ReservedSpace = 1+NumDests;
3099 NumOperands = 1;
3100 OperandList = allocHungoffUses(ReservedSpace);
3101
3102 OperandList[0] = Address;
3103}
3104
3105
3106/// resizeOperands - resize operands - This adjusts the length of the operands
3107/// list according to the following behavior:
3108/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3109/// of operation. This grows the number of ops by 2 times.
3110/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3111/// 3. If NumOps == NumOperands, trim the reserved space.
3112///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003113void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003114 unsigned e = getNumOperands();
3115 if (NumOps == 0) {
3116 NumOps = e*2;
3117 } else if (NumOps*2 > NumOperands) {
3118 // No resize needed.
3119 if (ReservedSpace >= NumOps) return;
3120 } else if (NumOps == NumOperands) {
3121 if (ReservedSpace == NumOps) return;
3122 } else {
3123 return;
3124 }
3125
3126 ReservedSpace = NumOps;
3127 Use *NewOps = allocHungoffUses(NumOps);
3128 Use *OldOps = OperandList;
3129 for (unsigned i = 0; i != e; ++i)
3130 NewOps[i] = OldOps[i];
3131 OperandList = NewOps;
3132 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3133}
3134
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003135IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3136 Instruction *InsertBefore)
3137: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003138 0, 0, InsertBefore) {
3139 init(Address, NumCases);
3140}
3141
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003142IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3143 BasicBlock *InsertAtEnd)
3144: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003145 0, 0, InsertAtEnd) {
3146 init(Address, NumCases);
3147}
3148
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003149IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3150 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003151 allocHungoffUses(IBI.getNumOperands()),
3152 IBI.getNumOperands()) {
3153 Use *OL = OperandList, *InOL = IBI.OperandList;
3154 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3155 OL[i] = InOL[i];
3156 SubclassOptionalData = IBI.SubclassOptionalData;
3157}
3158
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003159IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003160 dropHungoffUses(OperandList);
3161}
3162
3163/// addDestination - Add a destination.
3164///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003165void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003166 unsigned OpNo = NumOperands;
3167 if (OpNo+1 > ReservedSpace)
3168 resizeOperands(0); // Get more space!
3169 // Initialize some new operands.
3170 assert(OpNo < ReservedSpace && "Growing didn't work!");
3171 NumOperands = OpNo+1;
3172 OperandList[OpNo] = DestBB;
3173}
3174
3175/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003176/// indirectbr instruction.
3177void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003178 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3179
3180 unsigned NumOps = getNumOperands();
3181 Use *OL = OperandList;
3182
3183 // Replace this value with the last one.
3184 OL[idx+1] = OL[NumOps-1];
3185
3186 // Nuke the last value.
3187 OL[NumOps-1].set(0);
3188 NumOperands = NumOps-1;
3189}
3190
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003191BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003192 return getSuccessor(idx);
3193}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003194unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003195 return getNumSuccessors();
3196}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003197void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003198 setSuccessor(idx, B);
3199}
3200
3201//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003202// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003203//===----------------------------------------------------------------------===//
3204
Chris Lattnerf22be932004-10-15 23:52:53 +00003205// Define these methods here so vtables don't get emitted into every translation
3206// unit that uses these classes.
3207
Devang Patel11cf3f42009-10-27 22:16:29 +00003208GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3209 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003210}
3211
Devang Patel11cf3f42009-10-27 22:16:29 +00003212BinaryOperator *BinaryOperator::clone_impl() const {
3213 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003214}
3215
Devang Patel11cf3f42009-10-27 22:16:29 +00003216FCmpInst* FCmpInst::clone_impl() const {
3217 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003218}
3219
Devang Patel11cf3f42009-10-27 22:16:29 +00003220ICmpInst* ICmpInst::clone_impl() const {
3221 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003222}
3223
Devang Patel11cf3f42009-10-27 22:16:29 +00003224ExtractValueInst *ExtractValueInst::clone_impl() const {
3225 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003226}
3227
Devang Patel11cf3f42009-10-27 22:16:29 +00003228InsertValueInst *InsertValueInst::clone_impl() const {
3229 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003230}
3231
Devang Patel11cf3f42009-10-27 22:16:29 +00003232AllocaInst *AllocaInst::clone_impl() const {
3233 return new AllocaInst(getAllocatedType(),
3234 (Value*)getOperand(0),
3235 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003236}
3237
Devang Patel11cf3f42009-10-27 22:16:29 +00003238LoadInst *LoadInst::clone_impl() const {
3239 return new LoadInst(getOperand(0),
3240 Twine(), isVolatile(),
3241 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003242}
3243
Devang Patel11cf3f42009-10-27 22:16:29 +00003244StoreInst *StoreInst::clone_impl() const {
3245 return new StoreInst(getOperand(0), getOperand(1),
3246 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003247}
3248
Devang Patel11cf3f42009-10-27 22:16:29 +00003249TruncInst *TruncInst::clone_impl() const {
3250 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003251}
3252
Devang Patel11cf3f42009-10-27 22:16:29 +00003253ZExtInst *ZExtInst::clone_impl() const {
3254 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003255}
3256
Devang Patel11cf3f42009-10-27 22:16:29 +00003257SExtInst *SExtInst::clone_impl() const {
3258 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003259}
3260
Devang Patel11cf3f42009-10-27 22:16:29 +00003261FPTruncInst *FPTruncInst::clone_impl() const {
3262 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003263}
3264
Devang Patel11cf3f42009-10-27 22:16:29 +00003265FPExtInst *FPExtInst::clone_impl() const {
3266 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003267}
3268
Devang Patel11cf3f42009-10-27 22:16:29 +00003269UIToFPInst *UIToFPInst::clone_impl() const {
3270 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003271}
3272
Devang Patel11cf3f42009-10-27 22:16:29 +00003273SIToFPInst *SIToFPInst::clone_impl() const {
3274 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003275}
3276
Devang Patel11cf3f42009-10-27 22:16:29 +00003277FPToUIInst *FPToUIInst::clone_impl() const {
3278 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003279}
3280
Devang Patel11cf3f42009-10-27 22:16:29 +00003281FPToSIInst *FPToSIInst::clone_impl() const {
3282 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003283}
3284
Devang Patel11cf3f42009-10-27 22:16:29 +00003285PtrToIntInst *PtrToIntInst::clone_impl() const {
3286 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003287}
3288
Devang Patel11cf3f42009-10-27 22:16:29 +00003289IntToPtrInst *IntToPtrInst::clone_impl() const {
3290 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003291}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003292
Devang Patel11cf3f42009-10-27 22:16:29 +00003293BitCastInst *BitCastInst::clone_impl() const {
3294 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003295}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003296
Devang Patel11cf3f42009-10-27 22:16:29 +00003297CallInst *CallInst::clone_impl() const {
3298 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003299}
3300
Devang Patel11cf3f42009-10-27 22:16:29 +00003301SelectInst *SelectInst::clone_impl() const {
3302 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003303}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003304
Devang Patel11cf3f42009-10-27 22:16:29 +00003305VAArgInst *VAArgInst::clone_impl() const {
3306 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003307}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003308
Devang Patel11cf3f42009-10-27 22:16:29 +00003309ExtractElementInst *ExtractElementInst::clone_impl() const {
3310 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003311}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003312
Devang Patel11cf3f42009-10-27 22:16:29 +00003313InsertElementInst *InsertElementInst::clone_impl() const {
3314 return InsertElementInst::Create(getOperand(0),
3315 getOperand(1),
3316 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003317}
3318
Devang Patel11cf3f42009-10-27 22:16:29 +00003319ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3320 return new ShuffleVectorInst(getOperand(0),
3321 getOperand(1),
3322 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003323}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003324
Devang Patel11cf3f42009-10-27 22:16:29 +00003325PHINode *PHINode::clone_impl() const {
3326 return new PHINode(*this);
3327}
3328
3329ReturnInst *ReturnInst::clone_impl() const {
3330 return new(getNumOperands()) ReturnInst(*this);
3331}
3332
3333BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003334 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003335 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003336}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003337
Devang Patel11cf3f42009-10-27 22:16:29 +00003338SwitchInst *SwitchInst::clone_impl() const {
3339 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003340}
3341
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003342IndirectBrInst *IndirectBrInst::clone_impl() const {
3343 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003344}
3345
3346
Devang Patel11cf3f42009-10-27 22:16:29 +00003347InvokeInst *InvokeInst::clone_impl() const {
3348 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003349}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003350
Devang Patel11cf3f42009-10-27 22:16:29 +00003351UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003352 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003353 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003354}
3355
Devang Patel11cf3f42009-10-27 22:16:29 +00003356UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003357 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003358 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003359}