blob: bbea62bdeda45f0bc93add87a4eca5d3f55af81e [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
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000019#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026//===----------------------------------------------------------------------===//
27// CallSite Class
28//===----------------------------------------------------------------------===//
29
Gabor Greif1b9921a2009-01-11 22:33:22 +000030#define CALLSITE_DELEGATE_GETTER(METHOD) \
31 Instruction *II(getInstruction()); \
32 return isCall() \
33 ? cast<CallInst>(II)->METHOD \
34 : cast<InvokeInst>(II)->METHOD
35
36#define CALLSITE_DELEGATE_SETTER(METHOD) \
37 Instruction *II(getInstruction()); \
38 if (isCall()) \
39 cast<CallInst>(II)->METHOD; \
40 else \
41 cast<InvokeInst>(II)->METHOD
42
Duncan Sands85fab3a2008-02-18 17:32:13 +000043CallSite::CallSite(Instruction *C) {
44 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000045 I.setPointer(C);
46 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000047}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000048unsigned CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000049 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000050}
51void CallSite::setCallingConv(unsigned CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000052 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000053}
Devang Patel4c758ea2008-09-25 21:00:45 +000054const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000055 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000056}
Devang Patel4c758ea2008-09-25 21:00:45 +000057void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000058 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000059}
Devang Patelba3fa6c2008-09-23 23:03:40 +000060bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000061 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000062}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000063uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000064 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000065}
Duncan Sands38ef3a82007-12-03 20:06:50 +000066bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000067 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000068}
Duncan Sands78c88722008-07-08 08:38:44 +000069void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000070 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000071}
Duncan Sands38ef3a82007-12-03 20:06:50 +000072bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000073 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000074}
Duncan Sands78c88722008-07-08 08:38:44 +000075void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000076 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000077}
78bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000079 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000080}
81void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000082 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000083}
Duncan Sands3353ed02007-12-18 09:59:50 +000084bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000085 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000086}
Duncan Sandsaa31b922007-12-19 21:13:37 +000087void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000088 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000089}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000090
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000091bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000092 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
93 if (AI->get() == Arg)
94 return true;
95 return false;
96}
97
Gabor Greif1b9921a2009-01-11 22:33:22 +000098#undef CALLSITE_DELEGATE_GETTER
99#undef CALLSITE_DELEGATE_SETTER
100
Gordon Henriksen14a55692007-12-10 02:14:30 +0000101//===----------------------------------------------------------------------===//
102// TerminatorInst Class
103//===----------------------------------------------------------------------===//
104
105// Out of line virtual method, so the vtable, etc has a home.
106TerminatorInst::~TerminatorInst() {
107}
108
Gabor Greiff6caff662008-05-10 08:32:32 +0000109//===----------------------------------------------------------------------===//
110// UnaryInstruction Class
111//===----------------------------------------------------------------------===//
112
Gordon Henriksen14a55692007-12-10 02:14:30 +0000113// Out of line virtual method, so the vtable, etc has a home.
114UnaryInstruction::~UnaryInstruction() {
115}
116
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000118// SelectInst Class
119//===----------------------------------------------------------------------===//
120
121/// areInvalidOperands - Return a string if the specified operands are invalid
122/// for a select operation, otherwise return null.
123const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
124 if (Op1->getType() != Op2->getType())
125 return "both values to select must have same type";
126
127 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
128 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000129 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000130 return "vector select condition element type must be i1";
131 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
132 if (ET == 0)
133 return "selected values for vector select must be vectors";
134 if (ET->getNumElements() != VT->getNumElements())
135 return "vector select requires selected vectors to have "
136 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000137 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000138 return "select condition must be i1 or <n x i1>";
139 }
140 return 0;
141}
142
143
144//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145// PHINode Class
146//===----------------------------------------------------------------------===//
147
148PHINode::PHINode(const PHINode &PN)
149 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000150 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000151 ReservedSpace(PN.getNumOperands()) {
152 Use *OL = OperandList;
153 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000154 OL[i] = PN.getOperand(i);
155 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156 }
157}
158
Gordon Henriksen14a55692007-12-10 02:14:30 +0000159PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000160 if (OperandList)
161 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162}
163
164// removeIncomingValue - Remove an incoming value. This is useful if a
165// predecessor basic block is deleted.
166Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
167 unsigned NumOps = getNumOperands();
168 Use *OL = OperandList;
169 assert(Idx*2 < NumOps && "BB not in PHI node!");
170 Value *Removed = OL[Idx*2];
171
172 // Move everything after this operand down.
173 //
174 // FIXME: we could just swap with the end of the list, then erase. However,
175 // client might not expect this to happen. The code as it is thrashes the
176 // use/def lists, which is kinda lame.
177 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
178 OL[i-2] = OL[i];
179 OL[i-2+1] = OL[i+1];
180 }
181
182 // Nuke the last value.
183 OL[NumOps-2].set(0);
184 OL[NumOps-2+1].set(0);
185 NumOperands = NumOps-2;
186
187 // If the PHI node is dead, because it has zero entries, nuke it now.
188 if (NumOps == 2 && DeletePHIIfEmpty) {
189 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000190 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000191 eraseFromParent();
192 }
193 return Removed;
194}
195
196/// resizeOperands - resize operands - This adjusts the length of the operands
197/// list according to the following behavior:
198/// 1. If NumOps == 0, grow the operand list in response to a push_back style
199/// of operation. This grows the number of ops by 1.5 times.
200/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
201/// 3. If NumOps == NumOperands, trim the reserved space.
202///
203void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000204 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000205 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000206 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000207 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
208 } else if (NumOps*2 > NumOperands) {
209 // No resize needed.
210 if (ReservedSpace >= NumOps) return;
211 } else if (NumOps == NumOperands) {
212 if (ReservedSpace == NumOps) return;
213 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000214 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 }
216
217 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000218 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000219 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000220 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000221 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000222 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223}
224
Nate Begemanb3923212005-08-04 23:24:19 +0000225/// hasConstantValue - If the specified PHI node always merges together the same
226/// value, return the value, otherwise return null.
227///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000228Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000229 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000230 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000231 if (getIncomingValue(0) != this) // not X = phi X
232 return getIncomingValue(0);
233 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000234 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000235 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000236
Nate Begemanb3923212005-08-04 23:24:19 +0000237 // Otherwise if all of the incoming values are the same for the PHI, replace
238 // the PHI node with the incoming value.
239 //
240 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000241 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000242 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000243 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000244 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000245 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000246 if (InVal && getIncomingValue(i) != InVal)
247 return 0; // Not the same, bail out.
248 else
249 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000250 }
Nate Begemanb3923212005-08-04 23:24:19 +0000251
252 // The only case that could cause InVal to be null is if we have a PHI node
253 // that only has entries for itself. In this case, there is no entry into the
254 // loop, so kill the PHI.
255 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000256 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000257
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000258 // If we have a PHI node like phi(X, undef, X), where X is defined by some
259 // instruction, we cannot always return X as the result of the PHI node. Only
260 // do this if X is not an instruction (thus it must dominate the PHI block),
261 // or if the client is prepared to deal with this possibility.
262 if (HasUndefInput && !AllowNonDominatingInstruction)
263 if (Instruction *IV = dyn_cast<Instruction>(InVal))
264 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000265 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000266 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000267 return 0; // Cannot guarantee that InVal dominates this PHINode.
268
Nate Begemanb3923212005-08-04 23:24:19 +0000269 // All of the incoming values are the same, return the value now.
270 return InVal;
271}
272
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000273
274//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275// CallInst Implementation
276//===----------------------------------------------------------------------===//
277
Gordon Henriksen14a55692007-12-10 02:14:30 +0000278CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279}
280
Chris Lattner054ba2c2007-02-13 00:58:44 +0000281void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
283 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000284 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285
Misha Brukmanb1c93172005-04-21 23:48:37 +0000286 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000288 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289
Chris Lattner054ba2c2007-02-13 00:58:44 +0000290 assert((NumParams == FTy->getNumParams() ||
291 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000292 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000293 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000294 assert((i >= FTy->getNumParams() ||
295 FTy->getParamType(i) == Params[i]->getType()) &&
296 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000297 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000298 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299}
300
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000301void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000302 assert(NumOperands == 3 && "NumOperands not set up?");
303 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000304 OL[0] = Func;
305 OL[1] = Actual1;
306 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000307
308 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000310 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000312 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000313 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000315 assert((0 >= FTy->getNumParams() ||
316 FTy->getParamType(0) == Actual1->getType()) &&
317 "Calling a function with a bad signature!");
318 assert((1 >= FTy->getNumParams() ||
319 FTy->getParamType(1) == Actual2->getType()) &&
320 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321}
322
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000323void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000324 assert(NumOperands == 2 && "NumOperands not set up?");
325 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000326 OL[0] = Func;
327 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000328
329 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000331 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000333 assert((FTy->getNumParams() == 1 ||
334 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000336 assert((0 == FTy->getNumParams() ||
337 FTy->getParamType(0) == Actual->getType()) &&
338 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339}
340
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000341void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000342 assert(NumOperands == 1 && "NumOperands not set up?");
343 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000344 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000346 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000348 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000350 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351}
352
Daniel Dunbar4975db62009-07-25 04:41:11 +0000353CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000354 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
356 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000357 Instruction::Call,
358 OperandTraits<CallInst>::op_end(this) - 2,
359 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000361 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362}
363
Daniel Dunbar4975db62009-07-25 04:41:11 +0000364CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000365 BasicBlock *InsertAtEnd)
366 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
367 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000368 Instruction::Call,
369 OperandTraits<CallInst>::op_end(this) - 2,
370 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000372 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000374CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000375 Instruction *InsertBefore)
376 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
377 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000378 Instruction::Call,
379 OperandTraits<CallInst>::op_end(this) - 1,
380 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000382 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000383}
384
Daniel Dunbar4975db62009-07-25 04:41:11 +0000385CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000386 BasicBlock *InsertAtEnd)
387 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
388 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000389 Instruction::Call,
390 OperandTraits<CallInst>::op_end(this) - 1,
391 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000392 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000393 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000394}
395
Misha Brukmanb1c93172005-04-21 23:48:37 +0000396CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000397 : Instruction(CI.getType(), Instruction::Call,
398 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000399 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000400 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000401 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402 Use *OL = OperandList;
403 Use *InOL = CI.OperandList;
404 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000405 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406}
407
Devang Patel4c758ea2008-09-25 21:00:45 +0000408void CallInst::addAttribute(unsigned i, Attributes attr) {
409 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000410 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000411 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000412}
413
Devang Patel4c758ea2008-09-25 21:00:45 +0000414void CallInst::removeAttribute(unsigned i, Attributes attr) {
415 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000416 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000417 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000418}
419
Devang Patelba3fa6c2008-09-23 23:03:40 +0000420bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000421 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000422 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000423 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000424 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000425 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000426}
427
Duncan Sands5208d1a2007-11-28 17:07:01 +0000428
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000429//===----------------------------------------------------------------------===//
430// InvokeInst Implementation
431//===----------------------------------------------------------------------===//
432
433void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000434 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000435 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Bill Wendling4bb96e92009-03-13 21:15:59 +0000436 Use *OL = OperandList;
437 OL[0] = Fn;
438 OL[1] = IfNormal;
439 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000440 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000441 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000442 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000443
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000444 assert(((NumArgs == FTy->getNumParams()) ||
445 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000446 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000447
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000448 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000449 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000450 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000451 "Invoking a function with a bad signature!");
452
Bill Wendling4bb96e92009-03-13 21:15:59 +0000453 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000454 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000455}
456
Misha Brukmanb1c93172005-04-21 23:48:37 +0000457InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000458 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000459 OperandTraits<InvokeInst>::op_end(this)
460 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000461 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000462 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000463 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000464 Use *OL = OperandList, *InOL = II.OperandList;
465 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000466 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000467}
468
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000469BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
470 return getSuccessor(idx);
471}
472unsigned InvokeInst::getNumSuccessorsV() const {
473 return getNumSuccessors();
474}
475void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
476 return setSuccessor(idx, B);
477}
478
Devang Patelba3fa6c2008-09-23 23:03:40 +0000479bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000480 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000481 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000482 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000483 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000484 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000485}
486
Devang Patel4c758ea2008-09-25 21:00:45 +0000487void InvokeInst::addAttribute(unsigned i, Attributes attr) {
488 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000489 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000490 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000491}
492
Devang Patel4c758ea2008-09-25 21:00:45 +0000493void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
494 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000495 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000496 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000497}
498
Duncan Sands5208d1a2007-11-28 17:07:01 +0000499
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000500//===----------------------------------------------------------------------===//
501// ReturnInst Implementation
502//===----------------------------------------------------------------------===//
503
Chris Lattner2195fc42007-02-24 00:55:48 +0000504ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000505 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000506 OperandTraits<ReturnInst>::op_end(this) -
507 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000508 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000509 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000510 Op<0>() = RI.Op<0>();
Chris Lattner2195fc42007-02-24 00:55:48 +0000511}
512
Owen Anderson55f1c092009-08-13 21:58:54 +0000513ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
514 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000515 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
516 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000517 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000518 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000519}
Owen Anderson55f1c092009-08-13 21:58:54 +0000520ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
521 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000522 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
523 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000524 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000525 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000526}
Owen Anderson55f1c092009-08-13 21:58:54 +0000527ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
528 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000529 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000530}
531
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532unsigned ReturnInst::getNumSuccessorsV() const {
533 return getNumSuccessors();
534}
535
Devang Patelae682fb2008-02-26 17:56:20 +0000536/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
537/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000538void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000539 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540}
541
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000542BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000543 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000544 return 0;
545}
546
Devang Patel59643e52008-02-23 00:35:18 +0000547ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000548}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000549
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000550//===----------------------------------------------------------------------===//
551// UnwindInst Implementation
552//===----------------------------------------------------------------------===//
553
Owen Anderson55f1c092009-08-13 21:58:54 +0000554UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
555 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
556 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000557}
Owen Anderson55f1c092009-08-13 21:58:54 +0000558UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
559 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
560 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000561}
562
563
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000564unsigned UnwindInst::getNumSuccessorsV() const {
565 return getNumSuccessors();
566}
567
568void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000569 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000570}
571
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000573 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574 return 0;
575}
576
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000577//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000578// UnreachableInst Implementation
579//===----------------------------------------------------------------------===//
580
Owen Anderson55f1c092009-08-13 21:58:54 +0000581UnreachableInst::UnreachableInst(LLVMContext &Context,
582 Instruction *InsertBefore)
583 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
584 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000585}
Owen Anderson55f1c092009-08-13 21:58:54 +0000586UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
587 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
588 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000589}
590
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000591unsigned UnreachableInst::getNumSuccessorsV() const {
592 return getNumSuccessors();
593}
594
595void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000596 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000597}
598
599BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000600 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000601 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000602}
603
604//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000605// BranchInst Implementation
606//===----------------------------------------------------------------------===//
607
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608void BranchInst::AssertOK() {
609 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000610 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000611 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000612}
613
Chris Lattner2195fc42007-02-24 00:55:48 +0000614BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000615 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000616 OperandTraits<BranchInst>::op_end(this) - 1,
617 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000618 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000619 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000620}
621BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
622 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000623 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000624 OperandTraits<BranchInst>::op_end(this) - 3,
625 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000626 Op<-1>() = IfTrue;
627 Op<-2>() = IfFalse;
628 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000629#ifndef NDEBUG
630 AssertOK();
631#endif
632}
633
634BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000635 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000636 OperandTraits<BranchInst>::op_end(this) - 1,
637 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000638 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000639 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000640}
641
642BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
643 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000644 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000645 OperandTraits<BranchInst>::op_end(this) - 3,
646 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000647 Op<-1>() = IfTrue;
648 Op<-2>() = IfFalse;
649 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000650#ifndef NDEBUG
651 AssertOK();
652#endif
653}
654
655
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000657 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000658 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
659 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000660 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661 if (BI.getNumOperands() != 1) {
662 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000663 Op<-3>() = BI.Op<-3>();
664 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000665 }
666}
667
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000668
669Use* Use::getPrefix() {
670 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
671 if (PotentialPrefix.getOpaqueValue())
672 return 0;
673
674 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
675}
676
677BranchInst::~BranchInst() {
678 if (NumOperands == 1) {
679 if (Use *Prefix = OperandList->getPrefix()) {
680 Op<-1>() = 0;
681 //
682 // mark OperandList to have a special value for scrutiny
683 // by baseclass destructors and operator delete
684 OperandList = Prefix;
685 } else {
686 NumOperands = 3;
687 OperandList = op_begin();
688 }
689 }
690}
691
692
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
694 return getSuccessor(idx);
695}
696unsigned BranchInst::getNumSuccessorsV() const {
697 return getNumSuccessors();
698}
699void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
700 setSuccessor(idx, B);
701}
702
703
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000704//===----------------------------------------------------------------------===//
705// AllocationInst Implementation
706//===----------------------------------------------------------------------===//
707
Owen Andersonb6b25302009-07-14 23:09:55 +0000708static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000709 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000710 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000711 else {
712 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000713 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000714 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000715 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000716 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000717 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718}
719
Owen Anderson4fdeba92009-07-15 23:53:25 +0000720AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000721 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +0000723 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000724 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000725 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000726 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000727 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000728}
729
Owen Anderson4fdeba92009-07-15 23:53:25 +0000730AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000731 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732 BasicBlock *InsertAtEnd)
Owen Anderson4056ca92009-07-29 22:17:13 +0000733 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000734 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000735 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000736 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000737 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000738}
739
Gordon Henriksen14a55692007-12-10 02:14:30 +0000740// Out of line virtual method, so the vtable, etc has a home.
741AllocationInst::~AllocationInst() {
742}
743
Dan Gohmanaa583d72008-03-24 16:55:58 +0000744void AllocationInst::setAlignment(unsigned Align) {
745 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
746 SubclassData = Log2_32(Align) + 1;
747 assert(getAlignment() == Align && "Alignment representation error!");
748}
749
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000751 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
752 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000753 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000754}
755
756const Type *AllocationInst::getAllocatedType() const {
757 return getType()->getElementType();
758}
759
760AllocaInst::AllocaInst(const AllocaInst &AI)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000761 : AllocationInst(AI.getType()->getElementType(),
Owen Andersonb6b25302009-07-14 23:09:55 +0000762 (Value*)AI.getOperand(0), Instruction::Alloca,
763 AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000764}
765
Chris Lattner8b291e62008-11-26 02:54:17 +0000766/// isStaticAlloca - Return true if this alloca is in the entry block of the
767/// function and is a constant size. If so, the code generator will fold it
768/// into the prolog/epilog code, so it is basically free.
769bool AllocaInst::isStaticAlloca() const {
770 // Must be constant size.
771 if (!isa<ConstantInt>(getArraySize())) return false;
772
773 // Must be in the entry block.
774 const BasicBlock *Parent = getParent();
775 return Parent == &Parent->getParent()->front();
776}
777
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000778MallocInst::MallocInst(const MallocInst &MI)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000779 : AllocationInst(MI.getType()->getElementType(),
Owen Andersonb6b25302009-07-14 23:09:55 +0000780 (Value*)MI.getOperand(0), Instruction::Malloc,
781 MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000782}
783
784//===----------------------------------------------------------------------===//
785// FreeInst Implementation
786//===----------------------------------------------------------------------===//
787
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000788void FreeInst::AssertOK() {
789 assert(isa<PointerType>(getOperand(0)->getType()) &&
790 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000791}
792
793FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000794 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
795 Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000796 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797}
798
799FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000800 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
801 Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000802 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803}
804
805
806//===----------------------------------------------------------------------===//
807// LoadInst Implementation
808//===----------------------------------------------------------------------===//
809
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000811 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000813}
814
Daniel Dunbar4975db62009-07-25 04:41:11 +0000815LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000817 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000818 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000819 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000820 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000821 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000822}
823
Daniel Dunbar4975db62009-07-25 04:41:11 +0000824LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000825 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000826 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000827 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000828 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000829 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000830 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000831}
832
Daniel Dunbar4975db62009-07-25 04:41:11 +0000833LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000834 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000835 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000836 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000837 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000838 setAlignment(0);
839 AssertOK();
840 setName(Name);
841}
842
Daniel Dunbar4975db62009-07-25 04:41:11 +0000843LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000844 unsigned Align, Instruction *InsertBef)
845 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
846 Load, Ptr, InsertBef) {
847 setVolatile(isVolatile);
848 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000849 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000850 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851}
852
Daniel Dunbar4975db62009-07-25 04:41:11 +0000853LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000854 unsigned Align, BasicBlock *InsertAE)
855 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
856 Load, Ptr, InsertAE) {
857 setVolatile(isVolatile);
858 setAlignment(Align);
859 AssertOK();
860 setName(Name);
861}
862
Daniel Dunbar4975db62009-07-25 04:41:11 +0000863LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000864 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000865 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000866 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000867 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000868 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000869 AssertOK();
870 setName(Name);
871}
872
Daniel Dunbar27096822009-08-11 18:11:15 +0000873
874
875LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
876 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
877 Load, Ptr, InsertBef) {
878 setVolatile(false);
879 setAlignment(0);
880 AssertOK();
881 if (Name && Name[0]) setName(Name);
882}
883
884LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
885 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
886 Load, Ptr, InsertAE) {
887 setVolatile(false);
888 setAlignment(0);
889 AssertOK();
890 if (Name && Name[0]) setName(Name);
891}
892
893LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
894 Instruction *InsertBef)
895: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
896 Load, Ptr, InsertBef) {
897 setVolatile(isVolatile);
898 setAlignment(0);
899 AssertOK();
900 if (Name && Name[0]) setName(Name);
901}
902
903LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
904 BasicBlock *InsertAE)
905 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
906 Load, Ptr, InsertAE) {
907 setVolatile(isVolatile);
908 setAlignment(0);
909 AssertOK();
910 if (Name && Name[0]) setName(Name);
911}
912
Christopher Lamb84485702007-04-22 19:24:39 +0000913void LoadInst::setAlignment(unsigned Align) {
914 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
915 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
916}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000917
918//===----------------------------------------------------------------------===//
919// StoreInst Implementation
920//===----------------------------------------------------------------------===//
921
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000923 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924 assert(isa<PointerType>(getOperand(1)->getType()) &&
925 "Ptr must have pointer type!");
926 assert(getOperand(0)->getType() ==
927 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000928 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000929}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000930
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931
932StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000933 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000934 OperandTraits<StoreInst>::op_begin(this),
935 OperandTraits<StoreInst>::operands(this),
936 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000937 Op<0>() = val;
938 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000939 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000940 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941 AssertOK();
942}
943
944StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000945 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000946 OperandTraits<StoreInst>::op_begin(this),
947 OperandTraits<StoreInst>::operands(this),
948 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000949 Op<0>() = val;
950 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000951 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000952 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000953 AssertOK();
954}
955
Misha Brukmanb1c93172005-04-21 23:48:37 +0000956StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000957 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000958 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000959 OperandTraits<StoreInst>::op_begin(this),
960 OperandTraits<StoreInst>::operands(this),
961 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000962 Op<0>() = val;
963 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000964 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000965 setAlignment(0);
966 AssertOK();
967}
968
969StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
970 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000971 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000972 OperandTraits<StoreInst>::op_begin(this),
973 OperandTraits<StoreInst>::operands(this),
974 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000975 Op<0>() = val;
976 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000977 setVolatile(isVolatile);
978 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000979 AssertOK();
980}
981
Misha Brukmanb1c93172005-04-21 23:48:37 +0000982StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000983 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000984 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000985 OperandTraits<StoreInst>::op_begin(this),
986 OperandTraits<StoreInst>::operands(this),
987 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000988 Op<0>() = val;
989 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000990 setVolatile(isVolatile);
991 setAlignment(Align);
992 AssertOK();
993}
994
995StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000996 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000997 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000998 OperandTraits<StoreInst>::op_begin(this),
999 OperandTraits<StoreInst>::operands(this),
1000 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001001 Op<0>() = val;
1002 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001003 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001004 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001005 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001006}
1007
Christopher Lamb84485702007-04-22 19:24:39 +00001008void StoreInst::setAlignment(unsigned Align) {
1009 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1010 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1011}
1012
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001013//===----------------------------------------------------------------------===//
1014// GetElementPtrInst Implementation
1015//===----------------------------------------------------------------------===//
1016
Christopher Lambedf07882007-12-17 01:12:55 +00001017static unsigned retrieveAddrSpace(const Value *Val) {
1018 return cast<PointerType>(Val->getType())->getAddressSpace();
1019}
1020
Gabor Greif21ba1842008-06-06 20:28:12 +00001021void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001022 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001023 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1024 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001025 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001026
Chris Lattner79807c3d2007-01-31 19:47:18 +00001027 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001028 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001029
1030 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001031}
1032
Daniel Dunbar4975db62009-07-25 04:41:11 +00001033void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001034 assert(NumOperands == 2 && "NumOperands not initialized?");
1035 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001036 OL[0] = Ptr;
1037 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001038
1039 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001040}
1041
Gabor Greiff6caff662008-05-10 08:32:32 +00001042GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001043 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001044 OperandTraits<GetElementPtrInst>::op_end(this)
1045 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001046 GEPI.getNumOperands()) {
1047 Use *OL = OperandList;
1048 Use *GEPIOL = GEPI.OperandList;
1049 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001050 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001051}
1052
Chris Lattner82981202005-05-03 05:43:30 +00001053GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001054 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001055 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001056 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001057 GetElementPtr,
1058 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1059 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001060 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001061}
1062
1063GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001064 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001065 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001066 checkType(getIndexedType(Ptr->getType(),Idx)),
1067 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001068 GetElementPtr,
1069 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1070 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001071 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001072}
1073
Chris Lattner6090a422009-03-09 04:46:40 +00001074/// getIndexedType - Returns the type of the element that would be accessed with
1075/// a gep instruction with the specified parameters.
1076///
1077/// The Idxs pointer should point to a continuous piece of memory containing the
1078/// indices, either as Value* or uint64_t.
1079///
1080/// A null type is returned if the indices are invalid for the specified
1081/// pointer type.
1082///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001083template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001084static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1085 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001086 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1087 if (!PTy) return 0; // Type isn't a pointer type!
1088 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001089
Chris Lattner6090a422009-03-09 04:46:40 +00001090 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001091 if (NumIdx == 0)
1092 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001093
1094 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001095 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1096 // that contain opaque types) under the assumption that it will be resolved to
1097 // a sane type later.
1098 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001099 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001100
Dan Gohman1ecaf452008-05-31 00:58:22 +00001101 unsigned CurIdx = 1;
1102 for (; CurIdx != NumIdx; ++CurIdx) {
1103 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1104 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001105 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001106 if (!CT->indexValid(Index)) return 0;
1107 Agg = CT->getTypeAtIndex(Index);
1108
1109 // If the new type forwards to another type, then it is in the middle
1110 // of being refined to another type (and hence, may have dropped all
1111 // references to what it was using before). So, use the new forwarded
1112 // type.
1113 if (const Type *Ty = Agg->getForwardedType())
1114 Agg = Ty;
1115 }
1116 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001117}
1118
Matthijs Kooijman04468622008-07-29 08:46:11 +00001119const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1120 Value* const *Idxs,
1121 unsigned NumIdx) {
1122 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1123}
1124
1125const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1126 uint64_t const *Idxs,
1127 unsigned NumIdx) {
1128 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1129}
1130
Chris Lattner82981202005-05-03 05:43:30 +00001131const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1132 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1133 if (!PTy) return 0; // Type isn't a pointer type!
1134
1135 // Check the pointer index.
1136 if (!PTy->indexValid(Idx)) return 0;
1137
Chris Lattnerc2233332005-05-03 16:44:45 +00001138 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001139}
1140
Chris Lattner45f15572007-04-14 00:12:57 +00001141
1142/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1143/// zeros. If so, the result pointer and the first operand have the same
1144/// value, just potentially different types.
1145bool GetElementPtrInst::hasAllZeroIndices() const {
1146 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1147 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1148 if (!CI->isZero()) return false;
1149 } else {
1150 return false;
1151 }
1152 }
1153 return true;
1154}
1155
Chris Lattner27058292007-04-27 20:35:56 +00001156/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1157/// constant integers. If so, the result pointer and the first operand have
1158/// a constant offset between them.
1159bool GetElementPtrInst::hasAllConstantIndices() const {
1160 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1161 if (!isa<ConstantInt>(getOperand(i)))
1162 return false;
1163 }
1164 return true;
1165}
1166
Chris Lattner45f15572007-04-14 00:12:57 +00001167
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001168//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001169// ExtractElementInst Implementation
1170//===----------------------------------------------------------------------===//
1171
1172ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001173 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001174 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001175 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001176 ExtractElement,
1177 OperandTraits<ExtractElementInst>::op_begin(this),
1178 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001179 assert(isValidOperands(Val, Index) &&
1180 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001181 Op<0>() = Val;
1182 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001183 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001184}
1185
1186ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001187 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001188 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001189 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001190 ExtractElement,
1191 OperandTraits<ExtractElementInst>::op_begin(this),
1192 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001193 assert(isValidOperands(Val, Index) &&
1194 "Invalid extractelement instruction operands!");
1195
Gabor Greif2d3024d2008-05-26 21:33:52 +00001196 Op<0>() = Val;
1197 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001198 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001199}
1200
Chris Lattner65511ff2006-10-05 06:24:58 +00001201
Chris Lattner54865b32006-04-08 04:05:48 +00001202bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001203 if (!isa<VectorType>(Val->getType()) ||
1204 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001205 return false;
1206 return true;
1207}
1208
1209
Robert Bocchino23004482006-01-10 19:05:34 +00001210//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001211// InsertElementInst Implementation
1212//===----------------------------------------------------------------------===//
1213
Chris Lattner0875d942006-04-14 22:20:32 +00001214InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001215 : Instruction(IE.getType(), InsertElement,
1216 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001217 Op<0>() = IE.Op<0>();
1218 Op<1>() = IE.Op<1>();
1219 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001220}
Chris Lattner54865b32006-04-08 04:05:48 +00001221InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001222 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001223 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001224 : Instruction(Vec->getType(), InsertElement,
1225 OperandTraits<InsertElementInst>::op_begin(this),
1226 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001227 assert(isValidOperands(Vec, Elt, Index) &&
1228 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001229 Op<0>() = Vec;
1230 Op<1>() = Elt;
1231 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001232 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001233}
1234
Chris Lattner54865b32006-04-08 04:05:48 +00001235InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001236 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001237 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001238 : Instruction(Vec->getType(), InsertElement,
1239 OperandTraits<InsertElementInst>::op_begin(this),
1240 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001241 assert(isValidOperands(Vec, Elt, Index) &&
1242 "Invalid insertelement instruction operands!");
1243
Gabor Greif2d3024d2008-05-26 21:33:52 +00001244 Op<0>() = Vec;
1245 Op<1>() = Elt;
1246 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001247 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001248}
1249
Chris Lattner54865b32006-04-08 04:05:48 +00001250bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1251 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001252 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001253 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001254
Reid Spencerd84d35b2007-02-15 02:26:10 +00001255 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001256 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001257
Owen Anderson55f1c092009-08-13 21:58:54 +00001258 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001259 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001260 return true;
1261}
1262
1263
Robert Bocchinoca27f032006-01-17 20:07:22 +00001264//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001265// ShuffleVectorInst Implementation
1266//===----------------------------------------------------------------------===//
1267
Chris Lattner0875d942006-04-14 22:20:32 +00001268ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001269 : Instruction(SV.getType(), ShuffleVector,
1270 OperandTraits<ShuffleVectorInst>::op_begin(this),
1271 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001272 Op<0>() = SV.Op<0>();
1273 Op<1>() = SV.Op<1>();
1274 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001275}
1276
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001277ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001278 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001279 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001280: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001281 cast<VectorType>(Mask->getType())->getNumElements()),
1282 ShuffleVector,
1283 OperandTraits<ShuffleVectorInst>::op_begin(this),
1284 OperandTraits<ShuffleVectorInst>::operands(this),
1285 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001286 assert(isValidOperands(V1, V2, Mask) &&
1287 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001288 Op<0>() = V1;
1289 Op<1>() = V2;
1290 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001291 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001292}
1293
1294ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001295 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001296 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001297 : Instruction(V1->getType(), ShuffleVector,
1298 OperandTraits<ShuffleVectorInst>::op_begin(this),
1299 OperandTraits<ShuffleVectorInst>::operands(this),
1300 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001301 assert(isValidOperands(V1, V2, Mask) &&
1302 "Invalid shuffle vector instruction operands!");
1303
Gabor Greif2d3024d2008-05-26 21:33:52 +00001304 Op<0>() = V1;
1305 Op<1>() = V2;
1306 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001307 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001308}
1309
Mon P Wang25f01062008-11-10 04:46:22 +00001310bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001311 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001312 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001313 return false;
1314
1315 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1316 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001317 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001318 return false;
1319 return true;
1320}
1321
Chris Lattnerf724e342008-03-02 05:28:33 +00001322/// getMaskValue - Return the index from the shuffle mask for the specified
1323/// output result. This is either -1 if the element is undef or a number less
1324/// than 2*numelements.
1325int ShuffleVectorInst::getMaskValue(unsigned i) const {
1326 const Constant *Mask = cast<Constant>(getOperand(2));
1327 if (isa<UndefValue>(Mask)) return -1;
1328 if (isa<ConstantAggregateZero>(Mask)) return 0;
1329 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1330 assert(i < MaskCV->getNumOperands() && "Index out of range");
1331
1332 if (isa<UndefValue>(MaskCV->getOperand(i)))
1333 return -1;
1334 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1335}
1336
Dan Gohman12fce772008-05-15 19:50:34 +00001337//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001338// InsertValueInst Class
1339//===----------------------------------------------------------------------===//
1340
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001341void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001342 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001343 assert(NumOperands == 2 && "NumOperands not initialized?");
1344 Op<0>() = Agg;
1345 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001346
Dan Gohman1ecaf452008-05-31 00:58:22 +00001347 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001348 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001349}
1350
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001351void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001352 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001353 assert(NumOperands == 2 && "NumOperands not initialized?");
1354 Op<0>() = Agg;
1355 Op<1>() = Val;
1356
1357 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001358 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001359}
1360
1361InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001362 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001363 OperandTraits<InsertValueInst>::op_begin(this), 2),
1364 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001365 Op<0>() = IVI.getOperand(0);
1366 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001367}
1368
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001369InsertValueInst::InsertValueInst(Value *Agg,
1370 Value *Val,
1371 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001372 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001373 Instruction *InsertBefore)
1374 : Instruction(Agg->getType(), InsertValue,
1375 OperandTraits<InsertValueInst>::op_begin(this),
1376 2, InsertBefore) {
1377 init(Agg, Val, Idx, Name);
1378}
1379
1380InsertValueInst::InsertValueInst(Value *Agg,
1381 Value *Val,
1382 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001383 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001384 BasicBlock *InsertAtEnd)
1385 : Instruction(Agg->getType(), InsertValue,
1386 OperandTraits<InsertValueInst>::op_begin(this),
1387 2, InsertAtEnd) {
1388 init(Agg, Val, Idx, Name);
1389}
1390
Dan Gohman0752bff2008-05-23 00:36:11 +00001391//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001392// ExtractValueInst Class
1393//===----------------------------------------------------------------------===//
1394
Gabor Greifcbcc4952008-06-06 21:06:32 +00001395void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001396 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001397 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001398
Dan Gohman1ecaf452008-05-31 00:58:22 +00001399 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001400 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001401}
1402
Daniel Dunbar4975db62009-07-25 04:41:11 +00001403void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001404 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001405
1406 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001407 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001408}
1409
1410ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001411 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001412 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001413}
1414
Dan Gohman12fce772008-05-15 19:50:34 +00001415// getIndexedType - Returns the type of the element that would be extracted
1416// with an extractvalue instruction with the specified parameters.
1417//
1418// A null type is returned if the indices are invalid for the specified
1419// pointer type.
1420//
1421const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001422 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001423 unsigned NumIdx) {
1424 unsigned CurIdx = 0;
1425 for (; CurIdx != NumIdx; ++CurIdx) {
1426 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001427 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1428 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001429 if (!CT->indexValid(Index)) return 0;
1430 Agg = CT->getTypeAtIndex(Index);
1431
1432 // If the new type forwards to another type, then it is in the middle
1433 // of being refined to another type (and hence, may have dropped all
1434 // references to what it was using before). So, use the new forwarded
1435 // type.
1436 if (const Type *Ty = Agg->getForwardedType())
1437 Agg = Ty;
1438 }
1439 return CurIdx == NumIdx ? Agg : 0;
1440}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001441
Dan Gohman4a3125b2008-06-17 21:07:55 +00001442const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001443 unsigned Idx) {
1444 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001445}
1446
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001447//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001448// BinaryOperator Class
1449//===----------------------------------------------------------------------===//
1450
Dan Gohmana5b96452009-06-04 22:49:04 +00001451/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1452/// type is floating-point, to help provide compatibility with an older API.
1453///
1454static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1455 const Type *Ty) {
1456 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1457 if (Ty->isFPOrFPVector()) {
1458 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1459 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1460 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1461 }
1462 return iType;
1463}
1464
Chris Lattner2195fc42007-02-24 00:55:48 +00001465BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001466 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001467 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001468 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001469 OperandTraits<BinaryOperator>::op_begin(this),
1470 OperandTraits<BinaryOperator>::operands(this),
1471 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001472 Op<0>() = S1;
1473 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001474 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001475 setName(Name);
1476}
1477
1478BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001479 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001480 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001481 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001482 OperandTraits<BinaryOperator>::op_begin(this),
1483 OperandTraits<BinaryOperator>::operands(this),
1484 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001485 Op<0>() = S1;
1486 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001487 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001488 setName(Name);
1489}
1490
1491
1492void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001493 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001494 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001495 assert(LHS->getType() == RHS->getType() &&
1496 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001497#ifndef NDEBUG
1498 switch (iType) {
1499 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001500 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001501 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001502 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001503 assert(getType()->isIntOrIntVector() &&
1504 "Tried to create an integer operation on a non-integer type!");
1505 break;
1506 case FAdd: case FSub:
1507 case FMul:
1508 assert(getType() == LHS->getType() &&
1509 "Arithmetic operation should return same type as operands!");
1510 assert(getType()->isFPOrFPVector() &&
1511 "Tried to create a floating-point operation on a "
1512 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001513 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001514 case UDiv:
1515 case SDiv:
1516 assert(getType() == LHS->getType() &&
1517 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001518 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1519 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001520 "Incorrect operand type (not integer) for S/UDIV");
1521 break;
1522 case FDiv:
1523 assert(getType() == LHS->getType() &&
1524 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001525 assert(getType()->isFPOrFPVector() &&
1526 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001527 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001528 case URem:
1529 case SRem:
1530 assert(getType() == LHS->getType() &&
1531 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001532 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1533 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001534 "Incorrect operand type (not integer) for S/UREM");
1535 break;
1536 case FRem:
1537 assert(getType() == LHS->getType() &&
1538 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001539 assert(getType()->isFPOrFPVector() &&
1540 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001541 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001542 case Shl:
1543 case LShr:
1544 case AShr:
1545 assert(getType() == LHS->getType() &&
1546 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001547 assert((getType()->isInteger() ||
1548 (isa<VectorType>(getType()) &&
1549 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1550 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001551 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001552 case And: case Or:
1553 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001554 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001556 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001557 (isa<VectorType>(getType()) &&
1558 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001559 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001560 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001561 default:
1562 break;
1563 }
1564#endif
1565}
1566
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001567BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001568 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 Instruction *InsertBefore) {
1570 assert(S1->getType() == S2->getType() &&
1571 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001572 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001573}
1574
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001575BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001576 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001577 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001578 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001579 InsertAtEnd->getInstList().push_back(Res);
1580 return Res;
1581}
1582
Dan Gohman5476cfd2009-08-12 16:23:25 +00001583BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001584 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001585 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001586 return new BinaryOperator(Instruction::Sub,
1587 zero, Op,
1588 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001589}
1590
Dan Gohman5476cfd2009-08-12 16:23:25 +00001591BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001592 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001593 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001594 return new BinaryOperator(Instruction::Sub,
1595 zero, Op,
1596 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597}
1598
Dan Gohman5476cfd2009-08-12 16:23:25 +00001599BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001600 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001601 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001602 return new BinaryOperator(Instruction::FSub,
1603 zero, Op,
1604 Op->getType(), Name, InsertBefore);
1605}
1606
Dan Gohman5476cfd2009-08-12 16:23:25 +00001607BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001608 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001609 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001610 return new BinaryOperator(Instruction::FSub,
1611 zero, Op,
1612 Op->getType(), Name, InsertAtEnd);
1613}
1614
Dan Gohman5476cfd2009-08-12 16:23:25 +00001615BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001616 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001617 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001618 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001619 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001620 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001621 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001622 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001623 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001624 }
1625
1626 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001627 Op->getType(), Name, InsertBefore);
1628}
1629
Dan Gohman5476cfd2009-08-12 16:23:25 +00001630BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001631 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001632 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001633 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001634 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001635 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001636 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001637 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001638 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001639 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001640 }
1641
1642 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001643 Op->getType(), Name, InsertAtEnd);
1644}
1645
1646
1647// isConstantAllOnes - Helper function for several functions below
1648static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001649 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1650 return CI->isAllOnesValue();
1651 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1652 return CV->isAllOnesValue();
1653 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001654}
1655
Owen Andersonbb2501b2009-07-13 22:18:28 +00001656bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001657 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1658 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001659 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1660 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001661 return false;
1662}
1663
Owen Andersonbb2501b2009-07-13 22:18:28 +00001664bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001665 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1666 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001667 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1668 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001669 return false;
1670}
1671
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001672bool BinaryOperator::isNot(const Value *V) {
1673 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1674 return (Bop->getOpcode() == Instruction::Xor &&
1675 (isConstantAllOnes(Bop->getOperand(1)) ||
1676 isConstantAllOnes(Bop->getOperand(0))));
1677 return false;
1678}
1679
Chris Lattner2c7d1772005-04-24 07:28:37 +00001680Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001681 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001682}
1683
Chris Lattner2c7d1772005-04-24 07:28:37 +00001684const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1685 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001686}
1687
Dan Gohmana5b96452009-06-04 22:49:04 +00001688Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001689 return cast<BinaryOperator>(BinOp)->getOperand(1);
1690}
1691
1692const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1693 return getFNegArgument(const_cast<Value*>(BinOp));
1694}
1695
Chris Lattner2c7d1772005-04-24 07:28:37 +00001696Value *BinaryOperator::getNotArgument(Value *BinOp) {
1697 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1698 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1699 Value *Op0 = BO->getOperand(0);
1700 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001701 if (isConstantAllOnes(Op0)) return Op1;
1702
1703 assert(isConstantAllOnes(Op1));
1704 return Op0;
1705}
1706
Chris Lattner2c7d1772005-04-24 07:28:37 +00001707const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1708 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001709}
1710
1711
1712// swapOperands - Exchange the two operands to this instruction. This
1713// instruction is safe to use on any binary instruction and does not
1714// modify the semantics of the instruction. If the instruction is
1715// order dependent (SetLT f.e.) the opcode is changed.
1716//
1717bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001718 if (!isCommutative())
1719 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001720 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001721 return false;
1722}
1723
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001724//===----------------------------------------------------------------------===//
1725// CastInst Class
1726//===----------------------------------------------------------------------===//
1727
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001728// Just determine if this cast only deals with integral->integral conversion.
1729bool CastInst::isIntegerCast() const {
1730 switch (getOpcode()) {
1731 default: return false;
1732 case Instruction::ZExt:
1733 case Instruction::SExt:
1734 case Instruction::Trunc:
1735 return true;
1736 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001737 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001738 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001739}
1740
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001741bool CastInst::isLosslessCast() const {
1742 // Only BitCast can be lossless, exit fast if we're not BitCast
1743 if (getOpcode() != Instruction::BitCast)
1744 return false;
1745
1746 // Identity cast is always lossless
1747 const Type* SrcTy = getOperand(0)->getType();
1748 const Type* DstTy = getType();
1749 if (SrcTy == DstTy)
1750 return true;
1751
Reid Spencer8d9336d2006-12-31 05:26:44 +00001752 // Pointer to pointer is always lossless.
1753 if (isa<PointerType>(SrcTy))
1754 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001755 return false; // Other types have no identity values
1756}
1757
1758/// This function determines if the CastInst does not require any bits to be
1759/// changed in order to effect the cast. Essentially, it identifies cases where
1760/// no code gen is necessary for the cast, hence the name no-op cast. For
1761/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001762/// # bitcast i32* %x to i8*
1763/// # bitcast <2 x i32> %x to <4 x i16>
1764/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001765/// @brief Determine if a cast is a no-op.
1766bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1767 switch (getOpcode()) {
1768 default:
1769 assert(!"Invalid CastOp");
1770 case Instruction::Trunc:
1771 case Instruction::ZExt:
1772 case Instruction::SExt:
1773 case Instruction::FPTrunc:
1774 case Instruction::FPExt:
1775 case Instruction::UIToFP:
1776 case Instruction::SIToFP:
1777 case Instruction::FPToUI:
1778 case Instruction::FPToSI:
1779 return false; // These always modify bits
1780 case Instruction::BitCast:
1781 return true; // BitCast never modifies bits.
1782 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001783 return IntPtrTy->getScalarSizeInBits() ==
1784 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001785 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001786 return IntPtrTy->getScalarSizeInBits() ==
1787 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001788 }
1789}
1790
1791/// This function determines if a pair of casts can be eliminated and what
1792/// opcode should be used in the elimination. This assumes that there are two
1793/// instructions like this:
1794/// * %F = firstOpcode SrcTy %x to MidTy
1795/// * %S = secondOpcode MidTy %F to DstTy
1796/// The function returns a resultOpcode so these two casts can be replaced with:
1797/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1798/// If no such cast is permited, the function returns 0.
1799unsigned CastInst::isEliminableCastPair(
1800 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1801 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1802{
1803 // Define the 144 possibilities for these two cast instructions. The values
1804 // in this matrix determine what to do in a given situation and select the
1805 // case in the switch below. The rows correspond to firstOp, the columns
1806 // correspond to secondOp. In looking at the table below, keep in mind
1807 // the following cast properties:
1808 //
1809 // Size Compare Source Destination
1810 // Operator Src ? Size Type Sign Type Sign
1811 // -------- ------------ ------------------- ---------------------
1812 // TRUNC > Integer Any Integral Any
1813 // ZEXT < Integral Unsigned Integer Any
1814 // SEXT < Integral Signed Integer Any
1815 // FPTOUI n/a FloatPt n/a Integral Unsigned
1816 // FPTOSI n/a FloatPt n/a Integral Signed
1817 // UITOFP n/a Integral Unsigned FloatPt n/a
1818 // SITOFP n/a Integral Signed FloatPt n/a
1819 // FPTRUNC > FloatPt n/a FloatPt n/a
1820 // FPEXT < FloatPt n/a FloatPt n/a
1821 // PTRTOINT n/a Pointer n/a Integral Unsigned
1822 // INTTOPTR n/a Integral Unsigned Pointer n/a
1823 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001824 //
1825 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001826 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1827 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001828 // of the produced value (we no longer know the top-part is all zeros).
1829 // Further this conversion is often much more expensive for typical hardware,
1830 // and causes issues when building libgcc. We disallow fptosi+sext for the
1831 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001832 const unsigned numCastOps =
1833 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1834 static const uint8_t CastResults[numCastOps][numCastOps] = {
1835 // T F F U S F F P I B -+
1836 // R Z S P P I I T P 2 N T |
1837 // U E E 2 2 2 2 R E I T C +- secondOp
1838 // N X X U S F F N X N 2 V |
1839 // C T T I I P P C T T P T -+
1840 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1841 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1842 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001843 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1844 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001845 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1846 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1847 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1848 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1849 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1850 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1851 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1852 };
1853
1854 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1855 [secondOp-Instruction::CastOpsBegin];
1856 switch (ElimCase) {
1857 case 0:
1858 // categorically disallowed
1859 return 0;
1860 case 1:
1861 // allowed, use first cast's opcode
1862 return firstOp;
1863 case 2:
1864 // allowed, use second cast's opcode
1865 return secondOp;
1866 case 3:
1867 // no-op cast in second op implies firstOp as long as the DestTy
1868 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001869 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001870 return firstOp;
1871 return 0;
1872 case 4:
1873 // no-op cast in second op implies firstOp as long as the DestTy
1874 // is floating point
1875 if (DstTy->isFloatingPoint())
1876 return firstOp;
1877 return 0;
1878 case 5:
1879 // no-op cast in first op implies secondOp as long as the SrcTy
1880 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001881 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001882 return secondOp;
1883 return 0;
1884 case 6:
1885 // no-op cast in first op implies secondOp as long as the SrcTy
1886 // is a floating point
1887 if (SrcTy->isFloatingPoint())
1888 return secondOp;
1889 return 0;
1890 case 7: {
1891 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00001892 if (!IntPtrTy)
1893 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001894 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1895 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001896 if (MidSize >= PtrSize)
1897 return Instruction::BitCast;
1898 return 0;
1899 }
1900 case 8: {
1901 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1902 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1903 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001904 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1905 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001906 if (SrcSize == DstSize)
1907 return Instruction::BitCast;
1908 else if (SrcSize < DstSize)
1909 return firstOp;
1910 return secondOp;
1911 }
1912 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1913 return Instruction::ZExt;
1914 case 10:
1915 // fpext followed by ftrunc is allowed if the bit size returned to is
1916 // the same as the original, in which case its just a bitcast
1917 if (SrcTy == DstTy)
1918 return Instruction::BitCast;
1919 return 0; // If the types are not the same we can't eliminate it.
1920 case 11:
1921 // bitcast followed by ptrtoint is allowed as long as the bitcast
1922 // is a pointer to pointer cast.
1923 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1924 return secondOp;
1925 return 0;
1926 case 12:
1927 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1928 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1929 return firstOp;
1930 return 0;
1931 case 13: {
1932 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00001933 if (!IntPtrTy)
1934 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001935 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1936 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1937 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001938 if (SrcSize <= PtrSize && SrcSize == DstSize)
1939 return Instruction::BitCast;
1940 return 0;
1941 }
1942 case 99:
1943 // cast combination can't happen (error in input). This is for all cases
1944 // where the MidTy is not the same for the two cast instructions.
1945 assert(!"Invalid Cast Combination");
1946 return 0;
1947 default:
1948 assert(!"Error in CastResults table!!!");
1949 return 0;
1950 }
1951 return 0;
1952}
1953
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001954CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001955 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956 // Construct and return the appropriate CastInst subclass
1957 switch (op) {
1958 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1959 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1960 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1961 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1962 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1963 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1964 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1965 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1966 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1967 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1968 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1969 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1970 default:
1971 assert(!"Invalid opcode provided");
1972 }
1973 return 0;
1974}
1975
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001976CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001977 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001978 // Construct and return the appropriate CastInst subclass
1979 switch (op) {
1980 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1981 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1982 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1983 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1984 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1985 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1986 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1987 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1988 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1989 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1990 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1991 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1992 default:
1993 assert(!"Invalid opcode provided");
1994 }
1995 return 0;
1996}
1997
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001998CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001999 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002000 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002001 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002002 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2003 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002004}
2005
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002006CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002007 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002008 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002009 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002010 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2011 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002012}
2013
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002014CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002015 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002016 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002017 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002018 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2019 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002020}
2021
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002022CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002023 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002024 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002025 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002026 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2027 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002028}
2029
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002030CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002031 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002032 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002033 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002034 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2035 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002036}
2037
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002038CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002039 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002040 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002041 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002042 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2043 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002044}
2045
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002046CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002047 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002048 BasicBlock *InsertAtEnd) {
2049 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002050 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002051 "Invalid cast");
2052
Chris Lattner03c49532007-01-15 02:27:26 +00002053 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002054 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2055 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002056}
2057
2058/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002059CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002060 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002061 Instruction *InsertBefore) {
2062 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002063 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002064 "Invalid cast");
2065
Chris Lattner03c49532007-01-15 02:27:26 +00002066 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002067 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2068 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002069}
2070
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002071CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002072 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002073 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002074 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002075 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2076 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002077 Instruction::CastOps opcode =
2078 (SrcBits == DstBits ? Instruction::BitCast :
2079 (SrcBits > DstBits ? Instruction::Trunc :
2080 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002081 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002082}
2083
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002084CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002085 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002086 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002087 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2088 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002089 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2090 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002091 Instruction::CastOps opcode =
2092 (SrcBits == DstBits ? Instruction::BitCast :
2093 (SrcBits > DstBits ? Instruction::Trunc :
2094 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002095 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002096}
2097
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002098CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002099 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002100 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002101 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002102 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002103 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2104 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002105 Instruction::CastOps opcode =
2106 (SrcBits == DstBits ? Instruction::BitCast :
2107 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002108 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002109}
2110
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002111CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002112 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002113 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002114 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002115 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002116 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2117 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002118 Instruction::CastOps opcode =
2119 (SrcBits == DstBits ? Instruction::BitCast :
2120 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002121 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002122}
2123
Duncan Sands55e50902008-01-06 10:12:28 +00002124// Check whether it is valid to call getCastOpcode for these types.
2125// This routine must be kept in sync with getCastOpcode.
2126bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2127 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2128 return false;
2129
2130 if (SrcTy == DestTy)
2131 return true;
2132
2133 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002134 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2135 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002136
2137 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002138 if (DestTy->isInteger()) { // Casting to integral
2139 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002140 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002141 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002142 return true;
2143 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002144 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002145 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002146 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002147 return isa<PointerType>(SrcTy);
2148 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002149 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2150 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002151 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002152 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002153 return true;
2154 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002155 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002156 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002157 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002158 return false;
2159 }
2160 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002161 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002162 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002163 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002164 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002165 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002166 return DestPTy->getBitWidth() == SrcBits;
2167 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002168 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2169 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002170 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002171 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002172 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002173 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002174 return false;
2175 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002176 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002177 return false;
2178 }
2179}
2180
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181// Provide a way to get a "cast" where the cast opcode is inferred from the
2182// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002183// logic in the castIsValid function below. This axiom should hold:
2184// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2185// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002186// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002187// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002188Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002189CastInst::getCastOpcode(
2190 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002191 // Get the bit sizes, we'll need these
2192 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002193 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2194 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002195
Duncan Sands55e50902008-01-06 10:12:28 +00002196 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2197 "Only first class types are castable!");
2198
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002199 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002200 if (DestTy->isInteger()) { // Casting to integral
2201 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002202 if (DestBits < SrcBits)
2203 return Trunc; // int -> smaller int
2204 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002205 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002206 return SExt; // signed -> SEXT
2207 else
2208 return ZExt; // unsigned -> ZEXT
2209 } else {
2210 return BitCast; // Same size, No-op cast
2211 }
2212 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002213 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002214 return FPToSI; // FP -> sint
2215 else
2216 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002217 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002218 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002219 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002220 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002221 return BitCast; // Same size, no-op cast
2222 } else {
2223 assert(isa<PointerType>(SrcTy) &&
2224 "Casting from a value that is not first-class type");
2225 return PtrToInt; // ptr -> int
2226 }
2227 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002228 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002229 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002230 return SIToFP; // sint -> FP
2231 else
2232 return UIToFP; // uint -> FP
2233 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2234 if (DestBits < SrcBits) {
2235 return FPTrunc; // FP -> smaller FP
2236 } else if (DestBits > SrcBits) {
2237 return FPExt; // FP -> larger FP
2238 } else {
2239 return BitCast; // same size, no-op cast
2240 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002241 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002242 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002243 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002244 PTy = NULL;
2245 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002246 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002247 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002249 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2250 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002251 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002252 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002253 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002254 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002255 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002256 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002257 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002258 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002259 }
2260 } else if (isa<PointerType>(DestTy)) {
2261 if (isa<PointerType>(SrcTy)) {
2262 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002263 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264 return IntToPtr; // int -> ptr
2265 } else {
2266 assert(!"Casting pointer to other than pointer or int");
2267 }
2268 } else {
2269 assert(!"Casting to type that is not first-class");
2270 }
2271
2272 // If we fall through to here we probably hit an assertion cast above
2273 // and assertions are not turned on. Anything we return is an error, so
2274 // BitCast is as good a choice as any.
2275 return BitCast;
2276}
2277
2278//===----------------------------------------------------------------------===//
2279// CastInst SubClass Constructors
2280//===----------------------------------------------------------------------===//
2281
2282/// Check that the construction parameters for a CastInst are correct. This
2283/// could be broken out into the separate constructors but it is useful to have
2284/// it in one place and to eliminate the redundant code for getting the sizes
2285/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002286bool
2287CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002288
2289 // Check for type sanity on the arguments
2290 const Type *SrcTy = S->getType();
2291 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2292 return false;
2293
2294 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002295 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2296 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002297
2298 // Switch on the opcode provided
2299 switch (op) {
2300 default: return false; // This is an input error
2301 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002302 return SrcTy->isIntOrIntVector() &&
2303 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002304 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002305 return SrcTy->isIntOrIntVector() &&
2306 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002308 return SrcTy->isIntOrIntVector() &&
2309 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002311 return SrcTy->isFPOrFPVector() &&
2312 DstTy->isFPOrFPVector() &&
2313 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002315 return SrcTy->isFPOrFPVector() &&
2316 DstTy->isFPOrFPVector() &&
2317 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002320 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2321 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002322 return SVTy->getElementType()->isIntOrIntVector() &&
2323 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002324 SVTy->getNumElements() == DVTy->getNumElements();
2325 }
2326 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002327 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002328 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002330 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2331 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002332 return SVTy->getElementType()->isFPOrFPVector() &&
2333 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002334 SVTy->getNumElements() == DVTy->getNumElements();
2335 }
2336 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002337 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002339 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002341 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342 case Instruction::BitCast:
2343 // BitCast implies a no-op cast of type only. No bits change.
2344 // However, you can't cast pointers to anything but pointers.
2345 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2346 return false;
2347
Duncan Sands55e50902008-01-06 10:12:28 +00002348 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349 // these cases, the cast is okay if the source and destination bit widths
2350 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002351 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352 }
2353}
2354
2355TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002356 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002358 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359}
2360
2361TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002362 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002364 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365}
2366
2367ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002368 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002370 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002371}
2372
2373ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002374 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002376 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377}
2378SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002379 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002381 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382}
2383
Jeff Cohencc08c832006-12-02 02:22:01 +00002384SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002385 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002387 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388}
2389
2390FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002391 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002393 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002394}
2395
2396FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002397 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002399 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400}
2401
2402FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002403 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002405 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406}
2407
2408FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002409 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002411 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002412}
2413
2414UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002415 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002417 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418}
2419
2420UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002421 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002423 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424}
2425
2426SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002427 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002429 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430}
2431
2432SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002433 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002435 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436}
2437
2438FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002439 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002441 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442}
2443
2444FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002445 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002447 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002448}
2449
2450FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002451 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002453 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454}
2455
2456FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002457 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002459 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460}
2461
2462PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002463 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002465 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002466}
2467
2468PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002469 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002470) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002471 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472}
2473
2474IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002475 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002476) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002477 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478}
2479
2480IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002481 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002482) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002483 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484}
2485
2486BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002487 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002489 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490}
2491
2492BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002493 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002495 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002497
2498//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002499// CmpInst Classes
2500//===----------------------------------------------------------------------===//
2501
Nate Begemand2195702008-05-12 19:01:56 +00002502CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002503 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002504 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002505 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002506 OperandTraits<CmpInst>::op_begin(this),
2507 OperandTraits<CmpInst>::operands(this),
2508 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002509 Op<0>() = LHS;
2510 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002511 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002512 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002513}
Gabor Greiff6caff662008-05-10 08:32:32 +00002514
Nate Begemand2195702008-05-12 19:01:56 +00002515CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002516 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002517 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002518 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002519 OperandTraits<CmpInst>::op_begin(this),
2520 OperandTraits<CmpInst>::operands(this),
2521 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002522 Op<0>() = LHS;
2523 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002524 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002525 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002526}
2527
2528CmpInst *
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002529CmpInst::Create(LLVMContext &Context, OtherOps Op, unsigned short predicate,
2530 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002531 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002532 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002533 if (InsertBefore)
2534 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2535 S1, S2, Name);
2536 else
2537 return new ICmpInst(Context, CmpInst::Predicate(predicate),
2538 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002539 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002540
2541 if (InsertBefore)
2542 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2543 S1, S2, Name);
2544 else
2545 return new FCmpInst(Context, CmpInst::Predicate(predicate),
2546 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002547}
2548
2549CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002550CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002551 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002552 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002553 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2554 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002555 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002556 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2557 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002558}
2559
2560void CmpInst::swapOperands() {
2561 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2562 IC->swapOperands();
2563 else
2564 cast<FCmpInst>(this)->swapOperands();
2565}
2566
2567bool CmpInst::isCommutative() {
2568 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2569 return IC->isCommutative();
2570 return cast<FCmpInst>(this)->isCommutative();
2571}
2572
2573bool CmpInst::isEquality() {
2574 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2575 return IC->isEquality();
2576 return cast<FCmpInst>(this)->isEquality();
2577}
2578
2579
Dan Gohman4e724382008-05-31 02:47:54 +00002580CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002581 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002582 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002583 case ICMP_EQ: return ICMP_NE;
2584 case ICMP_NE: return ICMP_EQ;
2585 case ICMP_UGT: return ICMP_ULE;
2586 case ICMP_ULT: return ICMP_UGE;
2587 case ICMP_UGE: return ICMP_ULT;
2588 case ICMP_ULE: return ICMP_UGT;
2589 case ICMP_SGT: return ICMP_SLE;
2590 case ICMP_SLT: return ICMP_SGE;
2591 case ICMP_SGE: return ICMP_SLT;
2592 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002593
Dan Gohman4e724382008-05-31 02:47:54 +00002594 case FCMP_OEQ: return FCMP_UNE;
2595 case FCMP_ONE: return FCMP_UEQ;
2596 case FCMP_OGT: return FCMP_ULE;
2597 case FCMP_OLT: return FCMP_UGE;
2598 case FCMP_OGE: return FCMP_ULT;
2599 case FCMP_OLE: return FCMP_UGT;
2600 case FCMP_UEQ: return FCMP_ONE;
2601 case FCMP_UNE: return FCMP_OEQ;
2602 case FCMP_UGT: return FCMP_OLE;
2603 case FCMP_ULT: return FCMP_OGE;
2604 case FCMP_UGE: return FCMP_OLT;
2605 case FCMP_ULE: return FCMP_OGT;
2606 case FCMP_ORD: return FCMP_UNO;
2607 case FCMP_UNO: return FCMP_ORD;
2608 case FCMP_TRUE: return FCMP_FALSE;
2609 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002610 }
2611}
2612
Reid Spencer266e42b2006-12-23 06:05:41 +00002613ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2614 switch (pred) {
2615 default: assert(! "Unknown icmp predicate!");
2616 case ICMP_EQ: case ICMP_NE:
2617 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2618 return pred;
2619 case ICMP_UGT: return ICMP_SGT;
2620 case ICMP_ULT: return ICMP_SLT;
2621 case ICMP_UGE: return ICMP_SGE;
2622 case ICMP_ULE: return ICMP_SLE;
2623 }
2624}
2625
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002626ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2627 switch (pred) {
2628 default: assert(! "Unknown icmp predicate!");
2629 case ICMP_EQ: case ICMP_NE:
2630 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2631 return pred;
2632 case ICMP_SGT: return ICMP_UGT;
2633 case ICMP_SLT: return ICMP_ULT;
2634 case ICMP_SGE: return ICMP_UGE;
2635 case ICMP_SLE: return ICMP_ULE;
2636 }
2637}
2638
Reid Spencer266e42b2006-12-23 06:05:41 +00002639bool ICmpInst::isSignedPredicate(Predicate pred) {
2640 switch (pred) {
2641 default: assert(! "Unknown icmp predicate!");
2642 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2643 return true;
2644 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2645 case ICMP_UGE: case ICMP_ULE:
2646 return false;
2647 }
2648}
2649
Reid Spencer0286bc12007-02-28 22:00:54 +00002650/// Initialize a set of values that all satisfy the condition with C.
2651///
2652ConstantRange
2653ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2654 APInt Lower(C);
2655 APInt Upper(C);
2656 uint32_t BitWidth = C.getBitWidth();
2657 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002658 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002659 case ICmpInst::ICMP_EQ: Upper++; break;
2660 case ICmpInst::ICMP_NE: Lower++; break;
2661 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2662 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2663 case ICmpInst::ICMP_UGT:
2664 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2665 break;
2666 case ICmpInst::ICMP_SGT:
2667 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2668 break;
2669 case ICmpInst::ICMP_ULE:
2670 Lower = APInt::getMinValue(BitWidth); Upper++;
2671 break;
2672 case ICmpInst::ICMP_SLE:
2673 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2674 break;
2675 case ICmpInst::ICMP_UGE:
2676 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2677 break;
2678 case ICmpInst::ICMP_SGE:
2679 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2680 break;
2681 }
2682 return ConstantRange(Lower, Upper);
2683}
2684
Dan Gohman4e724382008-05-31 02:47:54 +00002685CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002686 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002687 default: assert(!"Unknown cmp predicate!");
2688 case ICMP_EQ: case ICMP_NE:
2689 return pred;
2690 case ICMP_SGT: return ICMP_SLT;
2691 case ICMP_SLT: return ICMP_SGT;
2692 case ICMP_SGE: return ICMP_SLE;
2693 case ICMP_SLE: return ICMP_SGE;
2694 case ICMP_UGT: return ICMP_ULT;
2695 case ICMP_ULT: return ICMP_UGT;
2696 case ICMP_UGE: return ICMP_ULE;
2697 case ICMP_ULE: return ICMP_UGE;
2698
Reid Spencerd9436b62006-11-20 01:22:35 +00002699 case FCMP_FALSE: case FCMP_TRUE:
2700 case FCMP_OEQ: case FCMP_ONE:
2701 case FCMP_UEQ: case FCMP_UNE:
2702 case FCMP_ORD: case FCMP_UNO:
2703 return pred;
2704 case FCMP_OGT: return FCMP_OLT;
2705 case FCMP_OLT: return FCMP_OGT;
2706 case FCMP_OGE: return FCMP_OLE;
2707 case FCMP_OLE: return FCMP_OGE;
2708 case FCMP_UGT: return FCMP_ULT;
2709 case FCMP_ULT: return FCMP_UGT;
2710 case FCMP_UGE: return FCMP_ULE;
2711 case FCMP_ULE: return FCMP_UGE;
2712 }
2713}
2714
Reid Spencer266e42b2006-12-23 06:05:41 +00002715bool CmpInst::isUnsigned(unsigned short predicate) {
2716 switch (predicate) {
2717 default: return false;
2718 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2719 case ICmpInst::ICMP_UGE: return true;
2720 }
2721}
2722
2723bool CmpInst::isSigned(unsigned short predicate){
2724 switch (predicate) {
2725 default: return false;
2726 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2727 case ICmpInst::ICMP_SGE: return true;
2728 }
2729}
2730
2731bool CmpInst::isOrdered(unsigned short predicate) {
2732 switch (predicate) {
2733 default: return false;
2734 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2735 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2736 case FCmpInst::FCMP_ORD: return true;
2737 }
2738}
2739
2740bool CmpInst::isUnordered(unsigned short predicate) {
2741 switch (predicate) {
2742 default: return false;
2743 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2744 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2745 case FCmpInst::FCMP_UNO: return true;
2746 }
2747}
2748
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002749//===----------------------------------------------------------------------===//
2750// SwitchInst Implementation
2751//===----------------------------------------------------------------------===//
2752
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002753void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002754 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002755 ReservedSpace = 2+NumCases*2;
2756 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002757 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002758
Gabor Greif2d3024d2008-05-26 21:33:52 +00002759 OperandList[0] = Value;
2760 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002761}
2762
Chris Lattner2195fc42007-02-24 00:55:48 +00002763/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2764/// switch on and a default destination. The number of additional cases can
2765/// be specified here to make memory allocation more efficient. This
2766/// constructor can also autoinsert before another instruction.
2767SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2768 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002769 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2770 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002771 init(Value, Default, NumCases);
2772}
2773
2774/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2775/// switch on and a default destination. The number of additional cases can
2776/// be specified here to make memory allocation more efficient. This
2777/// constructor also autoinserts at the end of the specified BasicBlock.
2778SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2779 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002780 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2781 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002782 init(Value, Default, NumCases);
2783}
2784
Misha Brukmanb1c93172005-04-21 23:48:37 +00002785SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002786 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002787 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002788 Use *OL = OperandList, *InOL = SI.OperandList;
2789 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002790 OL[i] = InOL[i];
2791 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002792 }
2793}
2794
Gordon Henriksen14a55692007-12-10 02:14:30 +00002795SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002796 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002797}
2798
2799
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002800/// addCase - Add an entry to the switch instruction...
2801///
Chris Lattner47ac1872005-02-24 05:32:09 +00002802void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002803 unsigned OpNo = NumOperands;
2804 if (OpNo+2 > ReservedSpace)
2805 resizeOperands(0); // Get more space!
2806 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002807 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002808 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002809 OperandList[OpNo] = OnVal;
2810 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002811}
2812
2813/// removeCase - This method removes the specified successor from the switch
2814/// instruction. Note that this cannot be used to remove the default
2815/// destination (successor #0).
2816///
2817void SwitchInst::removeCase(unsigned idx) {
2818 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002819 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2820
2821 unsigned NumOps = getNumOperands();
2822 Use *OL = OperandList;
2823
2824 // Move everything after this operand down.
2825 //
2826 // FIXME: we could just swap with the end of the list, then erase. However,
2827 // client might not expect this to happen. The code as it is thrashes the
2828 // use/def lists, which is kinda lame.
2829 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2830 OL[i-2] = OL[i];
2831 OL[i-2+1] = OL[i+1];
2832 }
2833
2834 // Nuke the last value.
2835 OL[NumOps-2].set(0);
2836 OL[NumOps-2+1].set(0);
2837 NumOperands = NumOps-2;
2838}
2839
2840/// resizeOperands - resize operands - This adjusts the length of the operands
2841/// list according to the following behavior:
2842/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002843/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002844/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2845/// 3. If NumOps == NumOperands, trim the reserved space.
2846///
2847void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002848 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002849 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002850 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002851 } else if (NumOps*2 > NumOperands) {
2852 // No resize needed.
2853 if (ReservedSpace >= NumOps) return;
2854 } else if (NumOps == NumOperands) {
2855 if (ReservedSpace == NumOps) return;
2856 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002857 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002858 }
2859
2860 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002861 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002862 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002863 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002864 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002865 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002866 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002867 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002868}
2869
2870
2871BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2872 return getSuccessor(idx);
2873}
2874unsigned SwitchInst::getNumSuccessorsV() const {
2875 return getNumSuccessors();
2876}
2877void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2878 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002879}
Chris Lattnerf22be932004-10-15 23:52:53 +00002880
Chris Lattnerf22be932004-10-15 23:52:53 +00002881// Define these methods here so vtables don't get emitted into every translation
2882// unit that uses these classes.
2883
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002884GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002885 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002886}
2887
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002888BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002889 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002890}
2891
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002892FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
2893 return new FCmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002894}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002895ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
2896 return new ICmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002897}
2898
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002899ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002900 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002901}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002902InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002903 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002904}
2905
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002906MallocInst *MallocInst::clone(LLVMContext&) const {
2907 return new MallocInst(*this);
2908}
Dan Gohman0752bff2008-05-23 00:36:11 +00002909
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002910AllocaInst *AllocaInst::clone(LLVMContext&) const {
2911 return new AllocaInst(*this);
2912}
2913
2914FreeInst *FreeInst::clone(LLVMContext&) const {
2915 return new FreeInst(getOperand(0));
2916}
2917
2918LoadInst *LoadInst::clone(LLVMContext&) const {
2919 return new LoadInst(*this);
2920}
2921
2922StoreInst *StoreInst::clone(LLVMContext&) const {
2923 return new StoreInst(*this);
2924}
2925
2926CastInst *TruncInst::clone(LLVMContext&) const {
2927 return new TruncInst(*this);
2928}
2929
2930CastInst *ZExtInst::clone(LLVMContext&) const {
2931 return new ZExtInst(*this);
2932}
2933
2934CastInst *SExtInst::clone(LLVMContext&) const {
2935 return new SExtInst(*this);
2936}
2937
2938CastInst *FPTruncInst::clone(LLVMContext&) const {
2939 return new FPTruncInst(*this);
2940}
2941
2942CastInst *FPExtInst::clone(LLVMContext&) const {
2943 return new FPExtInst(*this);
2944}
2945
2946CastInst *UIToFPInst::clone(LLVMContext&) const {
2947 return new UIToFPInst(*this);
2948}
2949
2950CastInst *SIToFPInst::clone(LLVMContext&) const {
2951 return new SIToFPInst(*this);
2952}
2953
2954CastInst *FPToUIInst::clone(LLVMContext&) const {
2955 return new FPToUIInst(*this);
2956}
2957
2958CastInst *FPToSIInst::clone(LLVMContext&) const {
2959 return new FPToSIInst(*this);
2960}
2961
2962CastInst *PtrToIntInst::clone(LLVMContext&) const {
2963 return new PtrToIntInst(*this);
2964}
2965
2966CastInst *IntToPtrInst::clone(LLVMContext&) const {
2967 return new IntToPtrInst(*this);
2968}
2969
2970CastInst *BitCastInst::clone(LLVMContext&) const {
2971 return new BitCastInst(*this);
2972}
2973
2974CallInst *CallInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002975 return new(getNumOperands()) CallInst(*this);
2976}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002977
2978SelectInst *SelectInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002979 return new(getNumOperands()) SelectInst(*this);
2980}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002981
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002982VAArgInst *VAArgInst::clone(LLVMContext&) const {
2983 return new VAArgInst(*this);
2984}
2985
2986ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Eric Christopherc9742252009-07-25 02:28:41 +00002987 return ExtractElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002988}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002989
2990InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002991 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002992}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002993
2994ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002995 return new ShuffleVectorInst(*this);
2996}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002997
2998PHINode *PHINode::clone(LLVMContext&) const {
2999 return new PHINode(*this);
3000}
3001
3002ReturnInst *ReturnInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003003 return new(getNumOperands()) ReturnInst(*this);
3004}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003005
3006BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003007 unsigned Ops(getNumOperands());
3008 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003009}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003010
3011SwitchInst *SwitchInst::clone(LLVMContext&) const {
3012 return new SwitchInst(*this);
3013}
3014
3015InvokeInst *InvokeInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003016 return new(getNumOperands()) InvokeInst(*this);
3017}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003018
Owen Anderson55f1c092009-08-13 21:58:54 +00003019UnwindInst *UnwindInst::clone(LLVMContext &C) const {
3020 return new UnwindInst(C);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003021}
3022
Owen Anderson55f1c092009-08-13 21:58:54 +00003023UnreachableInst *UnreachableInst::clone(LLVMContext &C) const {
3024 return new UnreachableInst(C);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003025}