blob: 381b85faa4c5cddc0025d3650c412017fdd81663 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Dan Gohman22571482009-09-03 15:34:35 +000022#include "llvm/Analysis/Dominators.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000025#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif9dc154b2010-07-01 10:41:37 +000036 ? (CallInst::ArgOffset
37 ? cast</*FIXME: CallInst*/User>(II)->op_begin()
38 : cast</*FIXME: CallInst*/User>(II)->op_end() - 1)
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000039 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Function
40}
41
Gordon Henriksen14a55692007-12-10 02:14:30 +000042//===----------------------------------------------------------------------===//
43// TerminatorInst Class
44//===----------------------------------------------------------------------===//
45
46// Out of line virtual method, so the vtable, etc has a home.
47TerminatorInst::~TerminatorInst() {
48}
49
Gabor Greiff6caff662008-05-10 08:32:32 +000050//===----------------------------------------------------------------------===//
51// UnaryInstruction Class
52//===----------------------------------------------------------------------===//
53
Gordon Henriksen14a55692007-12-10 02:14:30 +000054// Out of line virtual method, so the vtable, etc has a home.
55UnaryInstruction::~UnaryInstruction() {
56}
57
Chris Lattnerafdb3de2005-01-29 00:35:16 +000058//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000059// SelectInst Class
60//===----------------------------------------------------------------------===//
61
62/// areInvalidOperands - Return a string if the specified operands are invalid
63/// for a select operation, otherwise return null.
64const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
65 if (Op1->getType() != Op2->getType())
66 return "both values to select must have same type";
67
68 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
69 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000070 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000071 return "vector select condition element type must be i1";
72 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
73 if (ET == 0)
74 return "selected values for vector select must be vectors";
75 if (ET->getNumElements() != VT->getNumElements())
76 return "vector select requires selected vectors to have "
77 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000078 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000079 return "select condition must be i1 or <n x i1>";
80 }
81 return 0;
82}
83
84
85//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000086// PHINode Class
87//===----------------------------------------------------------------------===//
88
89PHINode::PHINode(const PHINode &PN)
90 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000091 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092 ReservedSpace(PN.getNumOperands()) {
93 Use *OL = OperandList;
94 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000095 OL[i] = PN.getOperand(i);
96 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Gordon Henriksen14a55692007-12-10 02:14:30 +0000101PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000102 if (OperandList)
103 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000104}
105
106// removeIncomingValue - Remove an incoming value. This is useful if a
107// predecessor basic block is deleted.
108Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
109 unsigned NumOps = getNumOperands();
110 Use *OL = OperandList;
111 assert(Idx*2 < NumOps && "BB not in PHI node!");
112 Value *Removed = OL[Idx*2];
113
114 // Move everything after this operand down.
115 //
116 // FIXME: we could just swap with the end of the list, then erase. However,
117 // client might not expect this to happen. The code as it is thrashes the
118 // use/def lists, which is kinda lame.
119 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
120 OL[i-2] = OL[i];
121 OL[i-2+1] = OL[i+1];
122 }
123
124 // Nuke the last value.
125 OL[NumOps-2].set(0);
126 OL[NumOps-2+1].set(0);
127 NumOperands = NumOps-2;
128
129 // If the PHI node is dead, because it has zero entries, nuke it now.
130 if (NumOps == 2 && DeletePHIIfEmpty) {
131 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000132 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000133 eraseFromParent();
134 }
135 return Removed;
136}
137
138/// resizeOperands - resize operands - This adjusts the length of the operands
139/// list according to the following behavior:
140/// 1. If NumOps == 0, grow the operand list in response to a push_back style
141/// of operation. This grows the number of ops by 1.5 times.
142/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
143/// 3. If NumOps == NumOperands, trim the reserved space.
144///
145void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000146 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000147 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000148 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
150 } else if (NumOps*2 > NumOperands) {
151 // No resize needed.
152 if (ReservedSpace >= NumOps) return;
153 } else if (NumOps == NumOperands) {
154 if (ReservedSpace == NumOps) return;
155 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000156 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157 }
158
159 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000161 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000162 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000163 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000164 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000165}
166
Nate Begemanb3923212005-08-04 23:24:19 +0000167/// hasConstantValue - If the specified PHI node always merges together the same
168/// value, return the value, otherwise return null.
169///
Dan Gohman22571482009-09-03 15:34:35 +0000170/// If the PHI has undef operands, but all the rest of the operands are
171/// some unique value, return that value if it can be proved that the
172/// value dominates the PHI. If DT is null, use a conservative check,
173/// otherwise use DT to test for dominance.
174///
175Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000176 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000177 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000178 if (getIncomingValue(0) != this) // not X = phi X
179 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000180 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000181 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000182
Nate Begemanb3923212005-08-04 23:24:19 +0000183 // Otherwise if all of the incoming values are the same for the PHI, replace
184 // the PHI node with the incoming value.
185 //
186 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000187 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000188 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000189 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000190 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000191 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000192 if (InVal && getIncomingValue(i) != InVal)
193 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000194 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000195 }
Nate Begemanb3923212005-08-04 23:24:19 +0000196
197 // The only case that could cause InVal to be null is if we have a PHI node
198 // that only has entries for itself. In this case, there is no entry into the
199 // loop, so kill the PHI.
200 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000201 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000202
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000203 // If we have a PHI node like phi(X, undef, X), where X is defined by some
204 // instruction, we cannot always return X as the result of the PHI node. Only
205 // do this if X is not an instruction (thus it must dominate the PHI block),
206 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000207 if (!HasUndefInput || !isa<Instruction>(InVal))
208 return InVal;
209
210 Instruction *IV = cast<Instruction>(InVal);
211 if (DT) {
212 // We have a DominatorTree. Do a precise test.
213 if (!DT->dominates(IV, this))
214 return 0;
215 } else {
216 // If it is in the entry block, it obviously dominates everything.
217 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
218 isa<InvokeInst>(IV))
219 return 0; // Cannot guarantee that InVal dominates this PHINode.
220 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000221
Nate Begemanb3923212005-08-04 23:24:19 +0000222 // All of the incoming values are the same, return the value now.
223 return InVal;
224}
225
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226
227//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000228// CallInst Implementation
229//===----------------------------------------------------------------------===//
230
Gordon Henriksen14a55692007-12-10 02:14:30 +0000231CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000232}
233
Chris Lattner054ba2c2007-02-13 00:58:44 +0000234void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000235 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greifeec74582010-06-29 11:41:38 +0000236 Op<ArgOffset -1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000237
Misha Brukmanb1c93172005-04-21 23:48:37 +0000238 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000240 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000241
Chris Lattner054ba2c2007-02-13 00:58:44 +0000242 assert((NumParams == FTy->getNumParams() ||
243 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000244 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000245 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000246 assert((i >= FTy->getNumParams() ||
247 FTy->getParamType(i) == Params[i]->getType()) &&
248 "Calling a function with a bad signature!");
Gabor Greifeec74582010-06-29 11:41:38 +0000249 OperandList[i + ArgOffset] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000250 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251}
252
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000253void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000254 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greifeec74582010-06-29 11:41:38 +0000255 Op<ArgOffset -1>() = Func;
256 Op<ArgOffset + 0>() = Actual1;
257 Op<ArgOffset + 1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000258
259 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000261 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000263 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000264 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000266 assert((0 >= FTy->getNumParams() ||
267 FTy->getParamType(0) == Actual1->getType()) &&
268 "Calling a function with a bad signature!");
269 assert((1 >= FTy->getNumParams() ||
270 FTy->getParamType(1) == Actual2->getType()) &&
271 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272}
273
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000274void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000275 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greifeec74582010-06-29 11:41:38 +0000276 Op<ArgOffset -1>() = Func;
277 Op<ArgOffset + 0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000278
279 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000283 assert((FTy->getNumParams() == 1 ||
284 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000286 assert((0 == FTy->getNumParams() ||
287 FTy->getParamType(0) == Actual->getType()) &&
288 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289}
290
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000291void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000292 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greifeec74582010-06-29 11:41:38 +0000293 Op<ArgOffset -1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000294
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000295 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000297 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000299 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Daniel Dunbar4975db62009-07-25 04:41:11 +0000302CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000303 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000306 Instruction::Call,
307 OperandTraits<CallInst>::op_end(this) - 2,
308 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000310 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311}
312
Daniel Dunbar4975db62009-07-25 04:41:11 +0000313CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314 BasicBlock *InsertAtEnd)
315 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
316 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000317 Instruction::Call,
318 OperandTraits<CallInst>::op_end(this) - 2,
319 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000321 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000323CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 Instruction *InsertBefore)
325 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
326 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000327 Instruction::Call,
328 OperandTraits<CallInst>::op_end(this) - 1,
329 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000331 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332}
333
Daniel Dunbar4975db62009-07-25 04:41:11 +0000334CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 BasicBlock *InsertAtEnd)
336 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
337 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000338 Instruction::Call,
339 OperandTraits<CallInst>::op_end(this) - 1,
340 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000342 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343}
344
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000346 : Instruction(CI.getType(), Instruction::Call,
347 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000348 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000349 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000350 setTailCall(CI.isTailCall());
351 setCallingConv(CI.getCallingConv());
352
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000353 Use *OL = OperandList;
354 Use *InOL = CI.OperandList;
355 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000356 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000357 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358}
359
Devang Patel4c758ea2008-09-25 21:00:45 +0000360void CallInst::addAttribute(unsigned i, Attributes attr) {
361 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000362 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000363 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000364}
365
Devang Patel4c758ea2008-09-25 21:00:45 +0000366void CallInst::removeAttribute(unsigned i, Attributes attr) {
367 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000368 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000369 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000370}
371
Devang Patelba3fa6c2008-09-23 23:03:40 +0000372bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000373 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000374 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000375 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000376 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000377 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000378}
379
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000380/// IsConstantOne - Return true only if val is constant int 1
381static bool IsConstantOne(Value *val) {
382 assert(val && "IsConstantOne does not work with NULL val");
383 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
384}
385
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000386static Instruction *createMalloc(Instruction *InsertBefore,
387 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000388 const Type *AllocTy, Value *AllocSize,
389 Value *ArraySize, Function *MallocF,
390 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000391 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000392 "createMalloc needs either InsertBefore or InsertAtEnd");
393
394 // malloc(type) becomes:
395 // bitcast (i8* malloc(typeSize)) to type*
396 // malloc(type, arraySize) becomes:
397 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000398 if (!ArraySize)
399 ArraySize = ConstantInt::get(IntPtrTy, 1);
400 else if (ArraySize->getType() != IntPtrTy) {
401 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000402 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
403 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000404 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000405 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
406 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000407 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000409 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000410 if (IsConstantOne(AllocSize)) {
411 AllocSize = ArraySize; // Operand * 1 = Operand
412 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
413 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
414 false /*ZExt*/);
415 // Malloc arg is constant product of type size and array size
416 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
417 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000418 // Multiply type size by the array size...
419 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000420 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
421 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000422 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000423 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
424 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000426 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427
Victor Hernandez788eaab2009-09-18 19:20:02 +0000428 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000429 // Create the call to Malloc.
430 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
431 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000432 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000433 Value *MallocFunc = MallocF;
434 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000435 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000436 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000437 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000438 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000439 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000440 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000441 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000442 Result = MCall;
443 if (Result->getType() != AllocPtrType)
444 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000445 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000446 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000447 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000448 Result = MCall;
449 if (Result->getType() != AllocPtrType) {
450 InsertAtEnd->getInstList().push_back(MCall);
451 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000452 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000453 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000454 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000455 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000456 if (Function *F = dyn_cast<Function>(MallocFunc)) {
457 MCall->setCallingConv(F->getCallingConv());
458 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
459 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000460 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000461
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000462 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000463}
464
465/// CreateMalloc - Generate the IR for a call to malloc:
466/// 1. Compute the malloc call's argument as the specified type's size,
467/// possibly multiplied by the array size if the array size is not
468/// constant 1.
469/// 2. Call malloc with that argument.
470/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000471Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
472 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 Value *AllocSize, Value *ArraySize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000474 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000475 const Twine &Name) {
476 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000477 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000478}
479
480/// CreateMalloc - Generate the IR for a call to malloc:
481/// 1. Compute the malloc call's argument as the specified type's size,
482/// possibly multiplied by the array size if the array size is not
483/// constant 1.
484/// 2. Call malloc with that argument.
485/// 3. Bitcast the result of the malloc call to the specified type.
486/// Note: This function does not add the bitcast to the basic block, that is the
487/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000488Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
489 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000490 Value *AllocSize, Value *ArraySize,
491 Function *MallocF, const Twine &Name) {
492 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000493 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000494}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000495
Victor Hernandeze2971492009-10-24 04:23:03 +0000496static Instruction* createFree(Value* Source, Instruction *InsertBefore,
497 BasicBlock *InsertAtEnd) {
498 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
499 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000500 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000501 "Can not free something of nonpointer type!");
502
503 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
504 Module* M = BB->getParent()->getParent();
505
506 const Type *VoidTy = Type::getVoidTy(M->getContext());
507 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
508 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000509 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000510 CallInst* Result = NULL;
511 Value *PtrCast = Source;
512 if (InsertBefore) {
513 if (Source->getType() != IntPtrTy)
514 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
515 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
516 } else {
517 if (Source->getType() != IntPtrTy)
518 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
519 Result = CallInst::Create(FreeFunc, PtrCast, "");
520 }
521 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000522 if (Function *F = dyn_cast<Function>(FreeFunc))
523 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000524
525 return Result;
526}
527
528/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000529Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
530 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000531}
532
533/// CreateFree - Generate the IR for a call to the builtin free function.
534/// Note: This function does not add the call to the basic block, that is the
535/// responsibility of the caller.
536Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
537 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
538 assert(FreeCall && "CreateFree did not create a CallInst");
539 return FreeCall;
540}
541
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542//===----------------------------------------------------------------------===//
543// InvokeInst Implementation
544//===----------------------------------------------------------------------===//
545
546void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000547 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000548 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000549 Op<-3>() = Fn;
550 Op<-2>() = IfNormal;
551 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000552 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000553 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000554 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000555
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000556 assert(((NumArgs == FTy->getNumParams()) ||
557 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000558 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000559
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000560 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000561 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000562 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000563 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000564 "Invoking a function with a bad signature!");
565
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000566 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000567 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000568}
569
Misha Brukmanb1c93172005-04-21 23:48:37 +0000570InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000572 OperandTraits<InvokeInst>::op_end(this)
573 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000574 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000575 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000576 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000577 Use *OL = OperandList, *InOL = II.OperandList;
578 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000579 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000580 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000581}
582
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000583BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
584 return getSuccessor(idx);
585}
586unsigned InvokeInst::getNumSuccessorsV() const {
587 return getNumSuccessors();
588}
589void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
590 return setSuccessor(idx, B);
591}
592
Devang Patelba3fa6c2008-09-23 23:03:40 +0000593bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000594 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000595 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000596 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000597 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000598 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000599}
600
Devang Patel4c758ea2008-09-25 21:00:45 +0000601void InvokeInst::addAttribute(unsigned i, Attributes attr) {
602 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000603 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000604 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000605}
606
Devang Patel4c758ea2008-09-25 21:00:45 +0000607void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
608 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000609 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000610 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000611}
612
Duncan Sands5208d1a2007-11-28 17:07:01 +0000613
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000614//===----------------------------------------------------------------------===//
615// ReturnInst Implementation
616//===----------------------------------------------------------------------===//
617
Chris Lattner2195fc42007-02-24 00:55:48 +0000618ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000619 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000620 OperandTraits<ReturnInst>::op_end(this) -
621 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000622 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000623 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000624 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000625 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000626}
627
Owen Anderson55f1c092009-08-13 21:58:54 +0000628ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
629 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000630 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
631 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000632 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000633 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000634}
Owen Anderson55f1c092009-08-13 21:58:54 +0000635ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
636 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000637 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
638 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000639 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000640 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000641}
Owen Anderson55f1c092009-08-13 21:58:54 +0000642ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
643 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000644 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000645}
646
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647unsigned ReturnInst::getNumSuccessorsV() const {
648 return getNumSuccessors();
649}
650
Devang Patelae682fb2008-02-26 17:56:20 +0000651/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
652/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000653void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000654 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000655}
656
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000658 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659 return 0;
660}
661
Devang Patel59643e52008-02-23 00:35:18 +0000662ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000663}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000664
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000665//===----------------------------------------------------------------------===//
666// UnwindInst Implementation
667//===----------------------------------------------------------------------===//
668
Owen Anderson55f1c092009-08-13 21:58:54 +0000669UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
670 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
671 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000672}
Owen Anderson55f1c092009-08-13 21:58:54 +0000673UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
674 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
675 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000676}
677
678
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679unsigned UnwindInst::getNumSuccessorsV() const {
680 return getNumSuccessors();
681}
682
683void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000684 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000685}
686
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000688 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000689 return 0;
690}
691
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000692//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000693// UnreachableInst Implementation
694//===----------------------------------------------------------------------===//
695
Owen Anderson55f1c092009-08-13 21:58:54 +0000696UnreachableInst::UnreachableInst(LLVMContext &Context,
697 Instruction *InsertBefore)
698 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
699 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000700}
Owen Anderson55f1c092009-08-13 21:58:54 +0000701UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
702 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
703 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
705
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706unsigned UnreachableInst::getNumSuccessorsV() const {
707 return getNumSuccessors();
708}
709
710void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000711 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000712}
713
714BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000715 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000716 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000717}
718
719//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720// BranchInst Implementation
721//===----------------------------------------------------------------------===//
722
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723void BranchInst::AssertOK() {
724 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000725 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000726 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727}
728
Chris Lattner2195fc42007-02-24 00:55:48 +0000729BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000730 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000731 OperandTraits<BranchInst>::op_end(this) - 1,
732 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000733 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000734 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000735}
736BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
737 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000738 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000739 OperandTraits<BranchInst>::op_end(this) - 3,
740 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000741 Op<-1>() = IfTrue;
742 Op<-2>() = IfFalse;
743 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000744#ifndef NDEBUG
745 AssertOK();
746#endif
747}
748
749BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000750 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000751 OperandTraits<BranchInst>::op_end(this) - 1,
752 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000753 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000754 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000755}
756
757BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
758 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000759 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000760 OperandTraits<BranchInst>::op_end(this) - 3,
761 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000762 Op<-1>() = IfTrue;
763 Op<-2>() = IfFalse;
764 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000765#ifndef NDEBUG
766 AssertOK();
767#endif
768}
769
770
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000772 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000773 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
774 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000775 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000776 if (BI.getNumOperands() != 1) {
777 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000778 Op<-3>() = BI.Op<-3>();
779 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000781 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000782}
783
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000784
785Use* Use::getPrefix() {
786 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
787 if (PotentialPrefix.getOpaqueValue())
788 return 0;
789
790 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
791}
792
793BranchInst::~BranchInst() {
794 if (NumOperands == 1) {
795 if (Use *Prefix = OperandList->getPrefix()) {
796 Op<-1>() = 0;
797 //
798 // mark OperandList to have a special value for scrutiny
799 // by baseclass destructors and operator delete
800 OperandList = Prefix;
801 } else {
802 NumOperands = 3;
803 OperandList = op_begin();
804 }
805 }
806}
807
808
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000809BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
810 return getSuccessor(idx);
811}
812unsigned BranchInst::getNumSuccessorsV() const {
813 return getNumSuccessors();
814}
815void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
816 setSuccessor(idx, B);
817}
818
819
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000821// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000822//===----------------------------------------------------------------------===//
823
Owen Andersonb6b25302009-07-14 23:09:55 +0000824static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000825 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000826 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000827 else {
828 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000829 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000830 assert(Amt->getType()->isIntegerTy() &&
831 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000832 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000833 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000834}
835
Victor Hernandez8acf2952009-10-23 21:09:37 +0000836AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
837 const Twine &Name, Instruction *InsertBefore)
838 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
839 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
840 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000841 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000842 setName(Name);
843}
844
845AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
846 const Twine &Name, BasicBlock *InsertAtEnd)
847 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
848 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
849 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000850 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000851 setName(Name);
852}
853
854AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
855 Instruction *InsertBefore)
856 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
857 getAISize(Ty->getContext(), 0), InsertBefore) {
858 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000859 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000860 setName(Name);
861}
862
863AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
864 BasicBlock *InsertAtEnd)
865 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
866 getAISize(Ty->getContext(), 0), InsertAtEnd) {
867 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000868 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000869 setName(Name);
870}
871
872AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
873 const Twine &Name, Instruction *InsertBefore)
874 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000875 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000876 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000877 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000878 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879}
880
Victor Hernandez8acf2952009-10-23 21:09:37 +0000881AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
882 const Twine &Name, BasicBlock *InsertAtEnd)
883 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000884 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000885 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000886 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000887 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000888}
889
Gordon Henriksen14a55692007-12-10 02:14:30 +0000890// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000891AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000892}
893
Victor Hernandez8acf2952009-10-23 21:09:37 +0000894void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000895 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000896 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000897 assert(getAlignment() == Align && "Alignment representation error!");
898}
899
Victor Hernandez8acf2952009-10-23 21:09:37 +0000900bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000901 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
902 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904}
905
Victor Hernandez8acf2952009-10-23 21:09:37 +0000906const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000907 return getType()->getElementType();
908}
909
Chris Lattner8b291e62008-11-26 02:54:17 +0000910/// isStaticAlloca - Return true if this alloca is in the entry block of the
911/// function and is a constant size. If so, the code generator will fold it
912/// into the prolog/epilog code, so it is basically free.
913bool AllocaInst::isStaticAlloca() const {
914 // Must be constant size.
915 if (!isa<ConstantInt>(getArraySize())) return false;
916
917 // Must be in the entry block.
918 const BasicBlock *Parent = getParent();
919 return Parent == &Parent->getParent()->front();
920}
921
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923// LoadInst Implementation
924//===----------------------------------------------------------------------===//
925
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000926void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000927 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000928 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929}
930
Daniel Dunbar4975db62009-07-25 04:41:11 +0000931LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000933 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000934 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000935 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000937 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000938}
939
Daniel Dunbar4975db62009-07-25 04:41:11 +0000940LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000942 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000943 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000944 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000945 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000946 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000947}
948
Daniel Dunbar4975db62009-07-25 04:41:11 +0000949LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000950 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000951 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000952 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000953 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000954 setAlignment(0);
955 AssertOK();
956 setName(Name);
957}
958
Daniel Dunbar4975db62009-07-25 04:41:11 +0000959LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000960 unsigned Align, Instruction *InsertBef)
961 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
962 Load, Ptr, InsertBef) {
963 setVolatile(isVolatile);
964 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000966 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000967}
968
Daniel Dunbar4975db62009-07-25 04:41:11 +0000969LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000970 unsigned Align, BasicBlock *InsertAE)
971 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
972 Load, Ptr, InsertAE) {
973 setVolatile(isVolatile);
974 setAlignment(Align);
975 AssertOK();
976 setName(Name);
977}
978
Daniel Dunbar4975db62009-07-25 04:41:11 +0000979LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000981 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000982 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000983 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000984 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000985 AssertOK();
986 setName(Name);
987}
988
Daniel Dunbar27096822009-08-11 18:11:15 +0000989
990
991LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
992 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
993 Load, Ptr, InsertBef) {
994 setVolatile(false);
995 setAlignment(0);
996 AssertOK();
997 if (Name && Name[0]) setName(Name);
998}
999
1000LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1001 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1002 Load, Ptr, InsertAE) {
1003 setVolatile(false);
1004 setAlignment(0);
1005 AssertOK();
1006 if (Name && Name[0]) setName(Name);
1007}
1008
1009LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1010 Instruction *InsertBef)
1011: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1012 Load, Ptr, InsertBef) {
1013 setVolatile(isVolatile);
1014 setAlignment(0);
1015 AssertOK();
1016 if (Name && Name[0]) setName(Name);
1017}
1018
1019LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1020 BasicBlock *InsertAE)
1021 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1022 Load, Ptr, InsertAE) {
1023 setVolatile(isVolatile);
1024 setAlignment(0);
1025 AssertOK();
1026 if (Name && Name[0]) setName(Name);
1027}
1028
Christopher Lamb84485702007-04-22 19:24:39 +00001029void LoadInst::setAlignment(unsigned Align) {
1030 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001031 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1032 ((Log2_32(Align)+1)<<1));
Christopher Lamb84485702007-04-22 19:24:39 +00001033}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034
1035//===----------------------------------------------------------------------===//
1036// StoreInst Implementation
1037//===----------------------------------------------------------------------===//
1038
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001039void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001040 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001041 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001042 "Ptr must have pointer type!");
1043 assert(getOperand(0)->getType() ==
1044 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001045 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001046}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001047
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001048
1049StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001050 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001051 OperandTraits<StoreInst>::op_begin(this),
1052 OperandTraits<StoreInst>::operands(this),
1053 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001054 Op<0>() = val;
1055 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001056 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001057 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001058 AssertOK();
1059}
1060
1061StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001062 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001063 OperandTraits<StoreInst>::op_begin(this),
1064 OperandTraits<StoreInst>::operands(this),
1065 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001066 Op<0>() = val;
1067 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001068 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001069 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001070 AssertOK();
1071}
1072
Misha Brukmanb1c93172005-04-21 23:48:37 +00001073StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001074 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001075 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001076 OperandTraits<StoreInst>::op_begin(this),
1077 OperandTraits<StoreInst>::operands(this),
1078 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001079 Op<0>() = val;
1080 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001081 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001082 setAlignment(0);
1083 AssertOK();
1084}
1085
1086StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1087 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001088 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001089 OperandTraits<StoreInst>::op_begin(this),
1090 OperandTraits<StoreInst>::operands(this),
1091 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001092 Op<0>() = val;
1093 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001094 setVolatile(isVolatile);
1095 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001096 AssertOK();
1097}
1098
Misha Brukmanb1c93172005-04-21 23:48:37 +00001099StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001100 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001101 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001102 OperandTraits<StoreInst>::op_begin(this),
1103 OperandTraits<StoreInst>::operands(this),
1104 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001105 Op<0>() = val;
1106 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001107 setVolatile(isVolatile);
1108 setAlignment(Align);
1109 AssertOK();
1110}
1111
1112StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001113 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001114 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001115 OperandTraits<StoreInst>::op_begin(this),
1116 OperandTraits<StoreInst>::operands(this),
1117 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001118 Op<0>() = val;
1119 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001120 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001121 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001122 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001123}
1124
Christopher Lamb84485702007-04-22 19:24:39 +00001125void StoreInst::setAlignment(unsigned Align) {
1126 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001127 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1128 ((Log2_32(Align)+1) << 1));
Christopher Lamb84485702007-04-22 19:24:39 +00001129}
1130
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001131//===----------------------------------------------------------------------===//
1132// GetElementPtrInst Implementation
1133//===----------------------------------------------------------------------===//
1134
Christopher Lambedf07882007-12-17 01:12:55 +00001135static unsigned retrieveAddrSpace(const Value *Val) {
1136 return cast<PointerType>(Val->getType())->getAddressSpace();
1137}
1138
Gabor Greif21ba1842008-06-06 20:28:12 +00001139void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001140 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001141 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1142 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001143 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001144
Chris Lattner79807c3d2007-01-31 19:47:18 +00001145 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001146 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001147
1148 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001149}
1150
Daniel Dunbar4975db62009-07-25 04:41:11 +00001151void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001152 assert(NumOperands == 2 && "NumOperands not initialized?");
1153 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001154 OL[0] = Ptr;
1155 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001156
1157 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001158}
1159
Gabor Greiff6caff662008-05-10 08:32:32 +00001160GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001161 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001162 OperandTraits<GetElementPtrInst>::op_end(this)
1163 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001164 GEPI.getNumOperands()) {
1165 Use *OL = OperandList;
1166 Use *GEPIOL = GEPI.OperandList;
1167 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001168 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001169 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001170}
1171
Chris Lattner82981202005-05-03 05:43:30 +00001172GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001173 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001174 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001175 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001176 GetElementPtr,
1177 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1178 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001179 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001180}
1181
1182GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001183 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001184 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001185 checkType(getIndexedType(Ptr->getType(),Idx)),
1186 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001187 GetElementPtr,
1188 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1189 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001190 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001191}
1192
Chris Lattner6090a422009-03-09 04:46:40 +00001193/// getIndexedType - Returns the type of the element that would be accessed with
1194/// a gep instruction with the specified parameters.
1195///
1196/// The Idxs pointer should point to a continuous piece of memory containing the
1197/// indices, either as Value* or uint64_t.
1198///
1199/// A null type is returned if the indices are invalid for the specified
1200/// pointer type.
1201///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001202template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001203static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1204 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001205 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1206 if (!PTy) return 0; // Type isn't a pointer type!
1207 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001208
Chris Lattner6090a422009-03-09 04:46:40 +00001209 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001210 if (NumIdx == 0)
1211 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001212
1213 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001214 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1215 // that contain opaque types) under the assumption that it will be resolved to
1216 // a sane type later.
1217 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001218 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001219
Dan Gohman1ecaf452008-05-31 00:58:22 +00001220 unsigned CurIdx = 1;
1221 for (; CurIdx != NumIdx; ++CurIdx) {
1222 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001223 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001224 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001225 if (!CT->indexValid(Index)) return 0;
1226 Agg = CT->getTypeAtIndex(Index);
1227
1228 // If the new type forwards to another type, then it is in the middle
1229 // of being refined to another type (and hence, may have dropped all
1230 // references to what it was using before). So, use the new forwarded
1231 // type.
1232 if (const Type *Ty = Agg->getForwardedType())
1233 Agg = Ty;
1234 }
1235 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001236}
1237
Matthijs Kooijman04468622008-07-29 08:46:11 +00001238const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1239 Value* const *Idxs,
1240 unsigned NumIdx) {
1241 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1242}
1243
1244const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1245 uint64_t const *Idxs,
1246 unsigned NumIdx) {
1247 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1248}
1249
Chris Lattner82981202005-05-03 05:43:30 +00001250const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1251 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1252 if (!PTy) return 0; // Type isn't a pointer type!
1253
1254 // Check the pointer index.
1255 if (!PTy->indexValid(Idx)) return 0;
1256
Chris Lattnerc2233332005-05-03 16:44:45 +00001257 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001258}
1259
Chris Lattner45f15572007-04-14 00:12:57 +00001260
1261/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1262/// zeros. If so, the result pointer and the first operand have the same
1263/// value, just potentially different types.
1264bool GetElementPtrInst::hasAllZeroIndices() const {
1265 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1266 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1267 if (!CI->isZero()) return false;
1268 } else {
1269 return false;
1270 }
1271 }
1272 return true;
1273}
1274
Chris Lattner27058292007-04-27 20:35:56 +00001275/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1276/// constant integers. If so, the result pointer and the first operand have
1277/// a constant offset between them.
1278bool GetElementPtrInst::hasAllConstantIndices() const {
1279 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1280 if (!isa<ConstantInt>(getOperand(i)))
1281 return false;
1282 }
1283 return true;
1284}
1285
Dan Gohman1b849082009-09-07 23:54:19 +00001286void GetElementPtrInst::setIsInBounds(bool B) {
1287 cast<GEPOperator>(this)->setIsInBounds(B);
1288}
Chris Lattner45f15572007-04-14 00:12:57 +00001289
Nick Lewycky28a5f252009-09-27 21:33:04 +00001290bool GetElementPtrInst::isInBounds() const {
1291 return cast<GEPOperator>(this)->isInBounds();
1292}
1293
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001294//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001295// ExtractElementInst Implementation
1296//===----------------------------------------------------------------------===//
1297
1298ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001299 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001300 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001301 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001302 ExtractElement,
1303 OperandTraits<ExtractElementInst>::op_begin(this),
1304 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001305 assert(isValidOperands(Val, Index) &&
1306 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001307 Op<0>() = Val;
1308 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001309 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001310}
1311
1312ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001313 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001314 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001315 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001316 ExtractElement,
1317 OperandTraits<ExtractElementInst>::op_begin(this),
1318 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001319 assert(isValidOperands(Val, Index) &&
1320 "Invalid extractelement instruction operands!");
1321
Gabor Greif2d3024d2008-05-26 21:33:52 +00001322 Op<0>() = Val;
1323 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001324 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001325}
1326
Chris Lattner65511ff2006-10-05 06:24:58 +00001327
Chris Lattner54865b32006-04-08 04:05:48 +00001328bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001329 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001330 return false;
1331 return true;
1332}
1333
1334
Robert Bocchino23004482006-01-10 19:05:34 +00001335//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001336// InsertElementInst Implementation
1337//===----------------------------------------------------------------------===//
1338
Chris Lattner54865b32006-04-08 04:05:48 +00001339InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001340 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001341 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001342 : Instruction(Vec->getType(), InsertElement,
1343 OperandTraits<InsertElementInst>::op_begin(this),
1344 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001345 assert(isValidOperands(Vec, Elt, Index) &&
1346 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001347 Op<0>() = Vec;
1348 Op<1>() = Elt;
1349 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001350 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001351}
1352
Chris Lattner54865b32006-04-08 04:05:48 +00001353InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001354 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001355 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001356 : Instruction(Vec->getType(), InsertElement,
1357 OperandTraits<InsertElementInst>::op_begin(this),
1358 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001359 assert(isValidOperands(Vec, Elt, Index) &&
1360 "Invalid insertelement instruction operands!");
1361
Gabor Greif2d3024d2008-05-26 21:33:52 +00001362 Op<0>() = Vec;
1363 Op<1>() = Elt;
1364 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001365 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001366}
1367
Chris Lattner54865b32006-04-08 04:05:48 +00001368bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1369 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001370 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001371 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001372
Reid Spencerd84d35b2007-02-15 02:26:10 +00001373 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001374 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001375
Duncan Sands9dff9be2010-02-15 16:12:20 +00001376 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001377 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001378 return true;
1379}
1380
1381
Robert Bocchinoca27f032006-01-17 20:07:22 +00001382//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001383// ShuffleVectorInst Implementation
1384//===----------------------------------------------------------------------===//
1385
1386ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001387 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001388 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001389: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001390 cast<VectorType>(Mask->getType())->getNumElements()),
1391 ShuffleVector,
1392 OperandTraits<ShuffleVectorInst>::op_begin(this),
1393 OperandTraits<ShuffleVectorInst>::operands(this),
1394 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001395 assert(isValidOperands(V1, V2, Mask) &&
1396 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001397 Op<0>() = V1;
1398 Op<1>() = V2;
1399 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001400 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001401}
1402
1403ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001404 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001405 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001406: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1407 cast<VectorType>(Mask->getType())->getNumElements()),
1408 ShuffleVector,
1409 OperandTraits<ShuffleVectorInst>::op_begin(this),
1410 OperandTraits<ShuffleVectorInst>::operands(this),
1411 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001412 assert(isValidOperands(V1, V2, Mask) &&
1413 "Invalid shuffle vector instruction operands!");
1414
Gabor Greif2d3024d2008-05-26 21:33:52 +00001415 Op<0>() = V1;
1416 Op<1>() = V2;
1417 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001418 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001419}
1420
Mon P Wang25f01062008-11-10 04:46:22 +00001421bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001422 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001423 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001424 return false;
1425
1426 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1427 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Duncan Sands9dff9be2010-02-15 16:12:20 +00001428 !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001429 return false;
1430 return true;
1431}
1432
Chris Lattnerf724e342008-03-02 05:28:33 +00001433/// getMaskValue - Return the index from the shuffle mask for the specified
1434/// output result. This is either -1 if the element is undef or a number less
1435/// than 2*numelements.
1436int ShuffleVectorInst::getMaskValue(unsigned i) const {
1437 const Constant *Mask = cast<Constant>(getOperand(2));
1438 if (isa<UndefValue>(Mask)) return -1;
1439 if (isa<ConstantAggregateZero>(Mask)) return 0;
1440 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1441 assert(i < MaskCV->getNumOperands() && "Index out of range");
1442
1443 if (isa<UndefValue>(MaskCV->getOperand(i)))
1444 return -1;
1445 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1446}
1447
Dan Gohman12fce772008-05-15 19:50:34 +00001448//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001449// InsertValueInst Class
1450//===----------------------------------------------------------------------===//
1451
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001452void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001453 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001454 assert(NumOperands == 2 && "NumOperands not initialized?");
1455 Op<0>() = Agg;
1456 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001457
Dan Gohmandd41bba2010-06-21 19:47:52 +00001458 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001459 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001460}
1461
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001462void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001463 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001464 assert(NumOperands == 2 && "NumOperands not initialized?");
1465 Op<0>() = Agg;
1466 Op<1>() = Val;
1467
1468 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001469 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001470}
1471
1472InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001473 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001474 OperandTraits<InsertValueInst>::op_begin(this), 2),
1475 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001476 Op<0>() = IVI.getOperand(0);
1477 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001478 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001479}
1480
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001481InsertValueInst::InsertValueInst(Value *Agg,
1482 Value *Val,
1483 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001484 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001485 Instruction *InsertBefore)
1486 : Instruction(Agg->getType(), InsertValue,
1487 OperandTraits<InsertValueInst>::op_begin(this),
1488 2, InsertBefore) {
1489 init(Agg, Val, Idx, Name);
1490}
1491
1492InsertValueInst::InsertValueInst(Value *Agg,
1493 Value *Val,
1494 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001495 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001496 BasicBlock *InsertAtEnd)
1497 : Instruction(Agg->getType(), InsertValue,
1498 OperandTraits<InsertValueInst>::op_begin(this),
1499 2, InsertAtEnd) {
1500 init(Agg, Val, Idx, Name);
1501}
1502
Dan Gohman0752bff2008-05-23 00:36:11 +00001503//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001504// ExtractValueInst Class
1505//===----------------------------------------------------------------------===//
1506
Gabor Greifcbcc4952008-06-06 21:06:32 +00001507void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001508 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001509 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001510
Dan Gohmandd41bba2010-06-21 19:47:52 +00001511 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001512 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001513}
1514
Daniel Dunbar4975db62009-07-25 04:41:11 +00001515void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001516 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001517
1518 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001519 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001520}
1521
1522ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001523 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001524 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001525 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001526}
1527
Dan Gohman12fce772008-05-15 19:50:34 +00001528// getIndexedType - Returns the type of the element that would be extracted
1529// with an extractvalue instruction with the specified parameters.
1530//
1531// A null type is returned if the indices are invalid for the specified
1532// pointer type.
1533//
1534const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001535 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001536 unsigned NumIdx) {
1537 unsigned CurIdx = 0;
1538 for (; CurIdx != NumIdx; ++CurIdx) {
1539 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001540 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001541 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001542 if (!CT->indexValid(Index)) return 0;
1543 Agg = CT->getTypeAtIndex(Index);
1544
1545 // If the new type forwards to another type, then it is in the middle
1546 // of being refined to another type (and hence, may have dropped all
1547 // references to what it was using before). So, use the new forwarded
1548 // type.
1549 if (const Type *Ty = Agg->getForwardedType())
1550 Agg = Ty;
1551 }
1552 return CurIdx == NumIdx ? Agg : 0;
1553}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001554
Dan Gohman4a3125b2008-06-17 21:07:55 +00001555const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001556 unsigned Idx) {
1557 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001558}
1559
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001560//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001561// BinaryOperator Class
1562//===----------------------------------------------------------------------===//
1563
Chris Lattner2195fc42007-02-24 00:55:48 +00001564BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001565 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001566 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001567 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001568 OperandTraits<BinaryOperator>::op_begin(this),
1569 OperandTraits<BinaryOperator>::operands(this),
1570 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001571 Op<0>() = S1;
1572 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001573 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001574 setName(Name);
1575}
1576
1577BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001578 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001579 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001580 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001581 OperandTraits<BinaryOperator>::op_begin(this),
1582 OperandTraits<BinaryOperator>::operands(this),
1583 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001584 Op<0>() = S1;
1585 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001586 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001587 setName(Name);
1588}
1589
1590
1591void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001592 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001593 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001594 assert(LHS->getType() == RHS->getType() &&
1595 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001596#ifndef NDEBUG
1597 switch (iType) {
1598 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001599 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001600 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001601 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001602 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001603 "Tried to create an integer operation on a non-integer type!");
1604 break;
1605 case FAdd: case FSub:
1606 case FMul:
1607 assert(getType() == LHS->getType() &&
1608 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001609 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001610 "Tried to create a floating-point operation on a "
1611 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001612 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001613 case UDiv:
1614 case SDiv:
1615 assert(getType() == LHS->getType() &&
1616 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001617 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001618 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001619 "Incorrect operand type (not integer) for S/UDIV");
1620 break;
1621 case FDiv:
1622 assert(getType() == LHS->getType() &&
1623 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001624 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001625 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001626 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001627 case URem:
1628 case SRem:
1629 assert(getType() == LHS->getType() &&
1630 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001631 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001632 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001633 "Incorrect operand type (not integer) for S/UREM");
1634 break;
1635 case FRem:
1636 assert(getType() == LHS->getType() &&
1637 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001638 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001639 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001640 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001641 case Shl:
1642 case LShr:
1643 case AShr:
1644 assert(getType() == LHS->getType() &&
1645 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001646 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001647 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001648 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001649 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001650 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001651 case And: case Or:
1652 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001653 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001654 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001655 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001656 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001657 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001658 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001659 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001660 default:
1661 break;
1662 }
1663#endif
1664}
1665
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001666BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001667 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001668 Instruction *InsertBefore) {
1669 assert(S1->getType() == S2->getType() &&
1670 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001671 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001672}
1673
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001674BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001675 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001677 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001678 InsertAtEnd->getInstList().push_back(Res);
1679 return Res;
1680}
1681
Dan Gohman5476cfd2009-08-12 16:23:25 +00001682BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001683 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001684 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001685 return new BinaryOperator(Instruction::Sub,
1686 zero, Op,
1687 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001688}
1689
Dan Gohman5476cfd2009-08-12 16:23:25 +00001690BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001691 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001692 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001693 return new BinaryOperator(Instruction::Sub,
1694 zero, Op,
1695 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696}
1697
Dan Gohman4ab44202009-12-18 02:58:50 +00001698BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1699 Instruction *InsertBefore) {
1700 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1701 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1702}
1703
1704BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1705 BasicBlock *InsertAtEnd) {
1706 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1707 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1708}
1709
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001710BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1711 Instruction *InsertBefore) {
1712 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1713 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1714}
1715
1716BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1717 BasicBlock *InsertAtEnd) {
1718 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1719 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1720}
1721
Dan Gohman5476cfd2009-08-12 16:23:25 +00001722BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001723 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001724 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001725 return new BinaryOperator(Instruction::FSub,
1726 zero, Op,
1727 Op->getType(), Name, InsertBefore);
1728}
1729
Dan Gohman5476cfd2009-08-12 16:23:25 +00001730BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001731 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001732 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001733 return new BinaryOperator(Instruction::FSub,
1734 zero, Op,
1735 Op->getType(), Name, InsertAtEnd);
1736}
1737
Dan Gohman5476cfd2009-08-12 16:23:25 +00001738BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001739 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001740 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001741 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001742 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001743 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001744 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001745 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001746 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001747 }
1748
1749 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001750 Op->getType(), Name, InsertBefore);
1751}
1752
Dan Gohman5476cfd2009-08-12 16:23:25 +00001753BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001754 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001755 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001756 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001757 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001758 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001759 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001760 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001761 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001762 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001763 }
1764
1765 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001766 Op->getType(), Name, InsertAtEnd);
1767}
1768
1769
1770// isConstantAllOnes - Helper function for several functions below
1771static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001772 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1773 return CI->isAllOnesValue();
1774 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1775 return CV->isAllOnesValue();
1776 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001777}
1778
Owen Andersonbb2501b2009-07-13 22:18:28 +00001779bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001780 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1781 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001782 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1783 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001784 return false;
1785}
1786
Owen Andersonbb2501b2009-07-13 22:18:28 +00001787bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001788 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1789 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001790 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001791 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001792 return false;
1793}
1794
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001795bool BinaryOperator::isNot(const Value *V) {
1796 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1797 return (Bop->getOpcode() == Instruction::Xor &&
1798 (isConstantAllOnes(Bop->getOperand(1)) ||
1799 isConstantAllOnes(Bop->getOperand(0))));
1800 return false;
1801}
1802
Chris Lattner2c7d1772005-04-24 07:28:37 +00001803Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001804 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001805}
1806
Chris Lattner2c7d1772005-04-24 07:28:37 +00001807const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1808 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001809}
1810
Dan Gohmana5b96452009-06-04 22:49:04 +00001811Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001812 return cast<BinaryOperator>(BinOp)->getOperand(1);
1813}
1814
1815const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1816 return getFNegArgument(const_cast<Value*>(BinOp));
1817}
1818
Chris Lattner2c7d1772005-04-24 07:28:37 +00001819Value *BinaryOperator::getNotArgument(Value *BinOp) {
1820 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1821 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1822 Value *Op0 = BO->getOperand(0);
1823 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001824 if (isConstantAllOnes(Op0)) return Op1;
1825
1826 assert(isConstantAllOnes(Op1));
1827 return Op0;
1828}
1829
Chris Lattner2c7d1772005-04-24 07:28:37 +00001830const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1831 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001832}
1833
1834
1835// swapOperands - Exchange the two operands to this instruction. This
1836// instruction is safe to use on any binary instruction and does not
1837// modify the semantics of the instruction. If the instruction is
1838// order dependent (SetLT f.e.) the opcode is changed.
1839//
1840bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001841 if (!isCommutative())
1842 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001843 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001844 return false;
1845}
1846
Dan Gohman1b849082009-09-07 23:54:19 +00001847void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1848 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1849}
1850
1851void BinaryOperator::setHasNoSignedWrap(bool b) {
1852 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1853}
1854
1855void BinaryOperator::setIsExact(bool b) {
1856 cast<SDivOperator>(this)->setIsExact(b);
1857}
1858
Nick Lewycky28a5f252009-09-27 21:33:04 +00001859bool BinaryOperator::hasNoUnsignedWrap() const {
1860 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1861}
1862
1863bool BinaryOperator::hasNoSignedWrap() const {
1864 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1865}
1866
1867bool BinaryOperator::isExact() const {
1868 return cast<SDivOperator>(this)->isExact();
1869}
1870
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001871//===----------------------------------------------------------------------===//
1872// CastInst Class
1873//===----------------------------------------------------------------------===//
1874
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875// Just determine if this cast only deals with integral->integral conversion.
1876bool CastInst::isIntegerCast() const {
1877 switch (getOpcode()) {
1878 default: return false;
1879 case Instruction::ZExt:
1880 case Instruction::SExt:
1881 case Instruction::Trunc:
1882 return true;
1883 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001884 return getOperand(0)->getType()->isIntegerTy() &&
1885 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001886 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001887}
1888
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889bool CastInst::isLosslessCast() const {
1890 // Only BitCast can be lossless, exit fast if we're not BitCast
1891 if (getOpcode() != Instruction::BitCast)
1892 return false;
1893
1894 // Identity cast is always lossless
1895 const Type* SrcTy = getOperand(0)->getType();
1896 const Type* DstTy = getType();
1897 if (SrcTy == DstTy)
1898 return true;
1899
Reid Spencer8d9336d2006-12-31 05:26:44 +00001900 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001901 if (SrcTy->isPointerTy())
1902 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001903 return false; // Other types have no identity values
1904}
1905
1906/// This function determines if the CastInst does not require any bits to be
1907/// changed in order to effect the cast. Essentially, it identifies cases where
1908/// no code gen is necessary for the cast, hence the name no-op cast. For
1909/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001910/// # bitcast i32* %x to i8*
1911/// # bitcast <2 x i32> %x to <4 x i16>
1912/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001913/// @brief Determine if the described cast is a no-op.
1914bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1915 const Type *SrcTy,
1916 const Type *DestTy,
1917 const Type *IntPtrTy) {
1918 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001919 default:
1920 assert(!"Invalid CastOp");
1921 case Instruction::Trunc:
1922 case Instruction::ZExt:
1923 case Instruction::SExt:
1924 case Instruction::FPTrunc:
1925 case Instruction::FPExt:
1926 case Instruction::UIToFP:
1927 case Instruction::SIToFP:
1928 case Instruction::FPToUI:
1929 case Instruction::FPToSI:
1930 return false; // These always modify bits
1931 case Instruction::BitCast:
1932 return true; // BitCast never modifies bits.
1933 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001934 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001935 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001937 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001938 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001939 }
1940}
1941
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001942/// @brief Determine if a cast is a no-op.
1943bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1944 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1945}
1946
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947/// This function determines if a pair of casts can be eliminated and what
1948/// opcode should be used in the elimination. This assumes that there are two
1949/// instructions like this:
1950/// * %F = firstOpcode SrcTy %x to MidTy
1951/// * %S = secondOpcode MidTy %F to DstTy
1952/// The function returns a resultOpcode so these two casts can be replaced with:
1953/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1954/// If no such cast is permited, the function returns 0.
1955unsigned CastInst::isEliminableCastPair(
1956 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1957 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1958{
1959 // Define the 144 possibilities for these two cast instructions. The values
1960 // in this matrix determine what to do in a given situation and select the
1961 // case in the switch below. The rows correspond to firstOp, the columns
1962 // correspond to secondOp. In looking at the table below, keep in mind
1963 // the following cast properties:
1964 //
1965 // Size Compare Source Destination
1966 // Operator Src ? Size Type Sign Type Sign
1967 // -------- ------------ ------------------- ---------------------
1968 // TRUNC > Integer Any Integral Any
1969 // ZEXT < Integral Unsigned Integer Any
1970 // SEXT < Integral Signed Integer Any
1971 // FPTOUI n/a FloatPt n/a Integral Unsigned
1972 // FPTOSI n/a FloatPt n/a Integral Signed
1973 // UITOFP n/a Integral Unsigned FloatPt n/a
1974 // SITOFP n/a Integral Signed FloatPt n/a
1975 // FPTRUNC > FloatPt n/a FloatPt n/a
1976 // FPEXT < FloatPt n/a FloatPt n/a
1977 // PTRTOINT n/a Pointer n/a Integral Unsigned
1978 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001979 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001980 //
1981 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001982 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1983 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001984 // of the produced value (we no longer know the top-part is all zeros).
1985 // Further this conversion is often much more expensive for typical hardware,
1986 // and causes issues when building libgcc. We disallow fptosi+sext for the
1987 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988 const unsigned numCastOps =
1989 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1990 static const uint8_t CastResults[numCastOps][numCastOps] = {
1991 // T F F U S F F P I B -+
1992 // R Z S P P I I T P 2 N T |
1993 // U E E 2 2 2 2 R E I T C +- secondOp
1994 // N X X U S F F N X N 2 V |
1995 // C T T I I P P C T T P T -+
1996 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1997 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1998 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001999 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2000 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002001 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2002 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2003 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2004 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2005 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2006 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2007 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2008 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002009
2010 // If either of the casts are a bitcast from scalar to vector, disallow the
2011 // merging.
2012 if ((firstOp == Instruction::BitCast &&
2013 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2014 (secondOp == Instruction::BitCast &&
2015 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2016 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002017
2018 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2019 [secondOp-Instruction::CastOpsBegin];
2020 switch (ElimCase) {
2021 case 0:
2022 // categorically disallowed
2023 return 0;
2024 case 1:
2025 // allowed, use first cast's opcode
2026 return firstOp;
2027 case 2:
2028 // allowed, use second cast's opcode
2029 return secondOp;
2030 case 3:
2031 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002032 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002033 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002034 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002035 return firstOp;
2036 return 0;
2037 case 4:
2038 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002039 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002040 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002041 return firstOp;
2042 return 0;
2043 case 5:
2044 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002045 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002046 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002047 return secondOp;
2048 return 0;
2049 case 6:
2050 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002051 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002052 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002053 return secondOp;
2054 return 0;
2055 case 7: {
2056 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002057 if (!IntPtrTy)
2058 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002059 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2060 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061 if (MidSize >= PtrSize)
2062 return Instruction::BitCast;
2063 return 0;
2064 }
2065 case 8: {
2066 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2067 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2068 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002069 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2070 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002071 if (SrcSize == DstSize)
2072 return Instruction::BitCast;
2073 else if (SrcSize < DstSize)
2074 return firstOp;
2075 return secondOp;
2076 }
2077 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2078 return Instruction::ZExt;
2079 case 10:
2080 // fpext followed by ftrunc is allowed if the bit size returned to is
2081 // the same as the original, in which case its just a bitcast
2082 if (SrcTy == DstTy)
2083 return Instruction::BitCast;
2084 return 0; // If the types are not the same we can't eliminate it.
2085 case 11:
2086 // bitcast followed by ptrtoint is allowed as long as the bitcast
2087 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002088 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002089 return secondOp;
2090 return 0;
2091 case 12:
2092 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002093 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 return firstOp;
2095 return 0;
2096 case 13: {
2097 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002098 if (!IntPtrTy)
2099 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002100 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2101 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2102 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103 if (SrcSize <= PtrSize && SrcSize == DstSize)
2104 return Instruction::BitCast;
2105 return 0;
2106 }
2107 case 99:
2108 // cast combination can't happen (error in input). This is for all cases
2109 // where the MidTy is not the same for the two cast instructions.
2110 assert(!"Invalid Cast Combination");
2111 return 0;
2112 default:
2113 assert(!"Error in CastResults table!!!");
2114 return 0;
2115 }
2116 return 0;
2117}
2118
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002119CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002120 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121 // Construct and return the appropriate CastInst subclass
2122 switch (op) {
2123 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2124 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2125 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2126 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2127 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2128 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2129 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2130 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2131 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2132 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2133 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2134 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2135 default:
2136 assert(!"Invalid opcode provided");
2137 }
2138 return 0;
2139}
2140
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002141CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002142 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002143 // Construct and return the appropriate CastInst subclass
2144 switch (op) {
2145 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2146 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2147 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2148 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2149 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2150 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2151 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2152 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2153 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2154 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2155 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2156 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2157 default:
2158 assert(!"Invalid opcode provided");
2159 }
2160 return 0;
2161}
2162
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002163CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002164 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002165 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002166 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002167 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2168 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002169}
2170
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002171CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002172 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002173 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002174 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002175 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2176 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002177}
2178
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002179CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002180 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002181 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002182 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002183 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2184 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002185}
2186
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002187CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002188 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002189 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002190 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002191 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2192 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002193}
2194
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002195CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002196 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002197 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002198 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002199 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2200 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002201}
2202
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002203CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002204 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002205 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002206 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002207 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2208 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002209}
2210
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002211CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002212 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002213 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002214 assert(S->getType()->isPointerTy() && "Invalid cast");
2215 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002216 "Invalid cast");
2217
Duncan Sands9dff9be2010-02-15 16:12:20 +00002218 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002219 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2220 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002221}
2222
2223/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002224CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002225 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002226 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002227 assert(S->getType()->isPointerTy() && "Invalid cast");
2228 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002229 "Invalid cast");
2230
Duncan Sands9dff9be2010-02-15 16:12:20 +00002231 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002232 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2233 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002234}
2235
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002236CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002237 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002238 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002239 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002240 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002241 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2242 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002243 Instruction::CastOps opcode =
2244 (SrcBits == DstBits ? Instruction::BitCast :
2245 (SrcBits > DstBits ? Instruction::Trunc :
2246 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002247 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002248}
2249
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002250CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002251 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002252 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002253 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002254 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002255 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2256 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002257 Instruction::CastOps opcode =
2258 (SrcBits == DstBits ? Instruction::BitCast :
2259 (SrcBits > DstBits ? Instruction::Trunc :
2260 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002261 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002262}
2263
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002264CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002265 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002266 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002267 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002268 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002269 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2270 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002271 Instruction::CastOps opcode =
2272 (SrcBits == DstBits ? Instruction::BitCast :
2273 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002274 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002275}
2276
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002277CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002278 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002279 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002280 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002281 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002282 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2283 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002284 Instruction::CastOps opcode =
2285 (SrcBits == DstBits ? Instruction::BitCast :
2286 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002287 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002288}
2289
Duncan Sands55e50902008-01-06 10:12:28 +00002290// Check whether it is valid to call getCastOpcode for these types.
2291// This routine must be kept in sync with getCastOpcode.
2292bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2293 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2294 return false;
2295
2296 if (SrcTy == DestTy)
2297 return true;
2298
2299 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002300 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2301 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002302
2303 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002304 if (DestTy->isIntegerTy()) { // Casting to integral
2305 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002306 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002307 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002308 return true;
2309 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002310 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002311 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002312 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002313 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002314 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002315 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2316 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002317 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002318 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002319 return true;
2320 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002321 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002322 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002323 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002324 return false;
2325 }
2326 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002327 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002328 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002329 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002330 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002331 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002332 return DestPTy->getBitWidth() == SrcBits;
2333 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002334 } else if (DestTy->isPointerTy()) { // Casting to pointer
2335 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002336 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002337 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002338 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002339 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002340 return false;
2341 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002342 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002343 return false;
2344 }
2345}
2346
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347// Provide a way to get a "cast" where the cast opcode is inferred from the
2348// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002349// logic in the castIsValid function below. This axiom should hold:
2350// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2351// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002353// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002355CastInst::getCastOpcode(
2356 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 // Get the bit sizes, we'll need these
2358 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002359 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2360 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361
Duncan Sands55e50902008-01-06 10:12:28 +00002362 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2363 "Only first class types are castable!");
2364
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002366 if (DestTy->isIntegerTy()) { // Casting to integral
2367 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368 if (DestBits < SrcBits)
2369 return Trunc; // int -> smaller int
2370 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002371 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372 return SExt; // signed -> SEXT
2373 else
2374 return ZExt; // unsigned -> ZEXT
2375 } else {
2376 return BitCast; // Same size, No-op cast
2377 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002378 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002379 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380 return FPToSI; // FP -> sint
2381 else
2382 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002383 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002385 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002386 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387 return BitCast; // Same size, no-op cast
2388 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002389 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390 "Casting from a value that is not first-class type");
2391 return PtrToInt; // ptr -> int
2392 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002393 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2394 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002395 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396 return SIToFP; // sint -> FP
2397 else
2398 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002399 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 if (DestBits < SrcBits) {
2401 return FPTrunc; // FP -> smaller FP
2402 } else if (DestBits > SrcBits) {
2403 return FPExt; // FP -> larger FP
2404 } else {
2405 return BitCast; // same size, no-op cast
2406 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002407 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002409 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002410 PTy = NULL;
2411 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002412 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002413 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002415 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2416 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002418 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002419 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002420 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002422 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002424 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002426 } else if (DestTy->isPointerTy()) {
2427 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002429 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430 return IntToPtr; // int -> ptr
2431 } else {
2432 assert(!"Casting pointer to other than pointer or int");
2433 }
2434 } else {
2435 assert(!"Casting to type that is not first-class");
2436 }
2437
2438 // If we fall through to here we probably hit an assertion cast above
2439 // and assertions are not turned on. Anything we return is an error, so
2440 // BitCast is as good a choice as any.
2441 return BitCast;
2442}
2443
2444//===----------------------------------------------------------------------===//
2445// CastInst SubClass Constructors
2446//===----------------------------------------------------------------------===//
2447
2448/// Check that the construction parameters for a CastInst are correct. This
2449/// could be broken out into the separate constructors but it is useful to have
2450/// it in one place and to eliminate the redundant code for getting the sizes
2451/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002452bool
2453CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454
2455 // Check for type sanity on the arguments
2456 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002457 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2458 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459 return false;
2460
2461 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002462 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2463 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464
2465 // Switch on the opcode provided
2466 switch (op) {
2467 default: return false; // This is an input error
2468 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002469 return SrcTy->isIntOrIntVectorTy() &&
2470 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002472 return SrcTy->isIntOrIntVectorTy() &&
2473 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002475 return SrcTy->isIntOrIntVectorTy() &&
2476 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002477 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002478 return SrcTy->isFPOrFPVectorTy() &&
2479 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002480 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002482 return SrcTy->isFPOrFPVectorTy() &&
2483 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002484 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002487 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2488 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002489 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2490 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002491 SVTy->getNumElements() == DVTy->getNumElements();
2492 }
2493 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002494 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002497 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2498 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002499 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2500 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002501 SVTy->getNumElements() == DVTy->getNumElements();
2502 }
2503 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002504 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002506 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002508 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509 case Instruction::BitCast:
2510 // BitCast implies a no-op cast of type only. No bits change.
2511 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002512 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513 return false;
2514
Duncan Sands55e50902008-01-06 10:12:28 +00002515 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 // these cases, the cast is okay if the source and destination bit widths
2517 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002518 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519 }
2520}
2521
2522TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002523 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002525 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526}
2527
2528TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002529 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002531 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532}
2533
2534ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002535 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002537 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538}
2539
2540ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002541 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002543 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544}
2545SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002546 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002548 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549}
2550
Jeff Cohencc08c832006-12-02 02:22:01 +00002551SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002552 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002554 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555}
2556
2557FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002558 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002560 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561}
2562
2563FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002564 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002566 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002567}
2568
2569FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002570 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002572 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573}
2574
2575FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002576 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002578 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579}
2580
2581UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002582 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002584 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585}
2586
2587UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002588 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002590 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591}
2592
2593SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002594 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002596 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597}
2598
2599SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002600 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002602 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603}
2604
2605FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002606 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002608 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609}
2610
2611FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002612 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002614 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615}
2616
2617FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002618 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002620 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621}
2622
2623FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002624 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002626 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002627}
2628
2629PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002630 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002632 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633}
2634
2635PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002636 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002638 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002639}
2640
2641IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002642 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002644 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645}
2646
2647IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002648 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002649) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002650 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002651}
2652
2653BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002654 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002656 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657}
2658
2659BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002660 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002662 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002663}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002664
2665//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002666// CmpInst Classes
2667//===----------------------------------------------------------------------===//
2668
Chris Lattneraec33da2010-01-22 06:25:37 +00002669void CmpInst::Anchor() const {}
2670
Nate Begemand2195702008-05-12 19:01:56 +00002671CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002672 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002673 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002674 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002675 OperandTraits<CmpInst>::op_begin(this),
2676 OperandTraits<CmpInst>::operands(this),
2677 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002678 Op<0>() = LHS;
2679 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002680 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002681 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002682}
Gabor Greiff6caff662008-05-10 08:32:32 +00002683
Nate Begemand2195702008-05-12 19:01:56 +00002684CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002685 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002686 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002687 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002688 OperandTraits<CmpInst>::op_begin(this),
2689 OperandTraits<CmpInst>::operands(this),
2690 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002691 Op<0>() = LHS;
2692 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002693 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002694 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002695}
2696
2697CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002698CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002699 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002700 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002701 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002702 if (InsertBefore)
2703 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2704 S1, S2, Name);
2705 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002706 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002707 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002708 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002709
2710 if (InsertBefore)
2711 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2712 S1, S2, Name);
2713 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002714 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002715 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002716}
2717
2718CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002719CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002720 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002721 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002722 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2723 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002724 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002725 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2726 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002727}
2728
2729void CmpInst::swapOperands() {
2730 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2731 IC->swapOperands();
2732 else
2733 cast<FCmpInst>(this)->swapOperands();
2734}
2735
2736bool CmpInst::isCommutative() {
2737 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2738 return IC->isCommutative();
2739 return cast<FCmpInst>(this)->isCommutative();
2740}
2741
2742bool CmpInst::isEquality() {
2743 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2744 return IC->isEquality();
2745 return cast<FCmpInst>(this)->isEquality();
2746}
2747
2748
Dan Gohman4e724382008-05-31 02:47:54 +00002749CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002750 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002751 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002752 case ICMP_EQ: return ICMP_NE;
2753 case ICMP_NE: return ICMP_EQ;
2754 case ICMP_UGT: return ICMP_ULE;
2755 case ICMP_ULT: return ICMP_UGE;
2756 case ICMP_UGE: return ICMP_ULT;
2757 case ICMP_ULE: return ICMP_UGT;
2758 case ICMP_SGT: return ICMP_SLE;
2759 case ICMP_SLT: return ICMP_SGE;
2760 case ICMP_SGE: return ICMP_SLT;
2761 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002762
Dan Gohman4e724382008-05-31 02:47:54 +00002763 case FCMP_OEQ: return FCMP_UNE;
2764 case FCMP_ONE: return FCMP_UEQ;
2765 case FCMP_OGT: return FCMP_ULE;
2766 case FCMP_OLT: return FCMP_UGE;
2767 case FCMP_OGE: return FCMP_ULT;
2768 case FCMP_OLE: return FCMP_UGT;
2769 case FCMP_UEQ: return FCMP_ONE;
2770 case FCMP_UNE: return FCMP_OEQ;
2771 case FCMP_UGT: return FCMP_OLE;
2772 case FCMP_ULT: return FCMP_OGE;
2773 case FCMP_UGE: return FCMP_OLT;
2774 case FCMP_ULE: return FCMP_OGT;
2775 case FCMP_ORD: return FCMP_UNO;
2776 case FCMP_UNO: return FCMP_ORD;
2777 case FCMP_TRUE: return FCMP_FALSE;
2778 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002779 }
2780}
2781
Reid Spencer266e42b2006-12-23 06:05:41 +00002782ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2783 switch (pred) {
2784 default: assert(! "Unknown icmp predicate!");
2785 case ICMP_EQ: case ICMP_NE:
2786 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2787 return pred;
2788 case ICMP_UGT: return ICMP_SGT;
2789 case ICMP_ULT: return ICMP_SLT;
2790 case ICMP_UGE: return ICMP_SGE;
2791 case ICMP_ULE: return ICMP_SLE;
2792 }
2793}
2794
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002795ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2796 switch (pred) {
2797 default: assert(! "Unknown icmp predicate!");
2798 case ICMP_EQ: case ICMP_NE:
2799 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2800 return pred;
2801 case ICMP_SGT: return ICMP_UGT;
2802 case ICMP_SLT: return ICMP_ULT;
2803 case ICMP_SGE: return ICMP_UGE;
2804 case ICMP_SLE: return ICMP_ULE;
2805 }
2806}
2807
Reid Spencer0286bc12007-02-28 22:00:54 +00002808/// Initialize a set of values that all satisfy the condition with C.
2809///
2810ConstantRange
2811ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2812 APInt Lower(C);
2813 APInt Upper(C);
2814 uint32_t BitWidth = C.getBitWidth();
2815 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002816 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002817 case ICmpInst::ICMP_EQ: Upper++; break;
2818 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002819 case ICmpInst::ICMP_ULT:
2820 Lower = APInt::getMinValue(BitWidth);
2821 // Check for an empty-set condition.
2822 if (Lower == Upper)
2823 return ConstantRange(BitWidth, /*isFullSet=*/false);
2824 break;
2825 case ICmpInst::ICMP_SLT:
2826 Lower = APInt::getSignedMinValue(BitWidth);
2827 // Check for an empty-set condition.
2828 if (Lower == Upper)
2829 return ConstantRange(BitWidth, /*isFullSet=*/false);
2830 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002831 case ICmpInst::ICMP_UGT:
2832 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002833 // Check for an empty-set condition.
2834 if (Lower == Upper)
2835 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002836 break;
2837 case ICmpInst::ICMP_SGT:
2838 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002839 // Check for an empty-set condition.
2840 if (Lower == Upper)
2841 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002842 break;
2843 case ICmpInst::ICMP_ULE:
2844 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002845 // Check for a full-set condition.
2846 if (Lower == Upper)
2847 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002848 break;
2849 case ICmpInst::ICMP_SLE:
2850 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002851 // Check for a full-set condition.
2852 if (Lower == Upper)
2853 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002854 break;
2855 case ICmpInst::ICMP_UGE:
2856 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002857 // Check for a full-set condition.
2858 if (Lower == Upper)
2859 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002860 break;
2861 case ICmpInst::ICMP_SGE:
2862 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002863 // Check for a full-set condition.
2864 if (Lower == Upper)
2865 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002866 break;
2867 }
2868 return ConstantRange(Lower, Upper);
2869}
2870
Dan Gohman4e724382008-05-31 02:47:54 +00002871CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002872 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002873 default: assert(!"Unknown cmp predicate!");
2874 case ICMP_EQ: case ICMP_NE:
2875 return pred;
2876 case ICMP_SGT: return ICMP_SLT;
2877 case ICMP_SLT: return ICMP_SGT;
2878 case ICMP_SGE: return ICMP_SLE;
2879 case ICMP_SLE: return ICMP_SGE;
2880 case ICMP_UGT: return ICMP_ULT;
2881 case ICMP_ULT: return ICMP_UGT;
2882 case ICMP_UGE: return ICMP_ULE;
2883 case ICMP_ULE: return ICMP_UGE;
2884
Reid Spencerd9436b62006-11-20 01:22:35 +00002885 case FCMP_FALSE: case FCMP_TRUE:
2886 case FCMP_OEQ: case FCMP_ONE:
2887 case FCMP_UEQ: case FCMP_UNE:
2888 case FCMP_ORD: case FCMP_UNO:
2889 return pred;
2890 case FCMP_OGT: return FCMP_OLT;
2891 case FCMP_OLT: return FCMP_OGT;
2892 case FCMP_OGE: return FCMP_OLE;
2893 case FCMP_OLE: return FCMP_OGE;
2894 case FCMP_UGT: return FCMP_ULT;
2895 case FCMP_ULT: return FCMP_UGT;
2896 case FCMP_UGE: return FCMP_ULE;
2897 case FCMP_ULE: return FCMP_UGE;
2898 }
2899}
2900
Reid Spencer266e42b2006-12-23 06:05:41 +00002901bool CmpInst::isUnsigned(unsigned short predicate) {
2902 switch (predicate) {
2903 default: return false;
2904 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2905 case ICmpInst::ICMP_UGE: return true;
2906 }
2907}
2908
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002909bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002910 switch (predicate) {
2911 default: return false;
2912 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2913 case ICmpInst::ICMP_SGE: return true;
2914 }
2915}
2916
2917bool CmpInst::isOrdered(unsigned short predicate) {
2918 switch (predicate) {
2919 default: return false;
2920 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2921 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2922 case FCmpInst::FCMP_ORD: return true;
2923 }
2924}
2925
2926bool CmpInst::isUnordered(unsigned short predicate) {
2927 switch (predicate) {
2928 default: return false;
2929 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2930 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2931 case FCmpInst::FCMP_UNO: return true;
2932 }
2933}
2934
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002935bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2936 switch(predicate) {
2937 default: return false;
2938 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2939 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2940 }
2941}
2942
2943bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2944 switch(predicate) {
2945 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2946 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2947 default: return false;
2948 }
2949}
2950
2951
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002952//===----------------------------------------------------------------------===//
2953// SwitchInst Implementation
2954//===----------------------------------------------------------------------===//
2955
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002956void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002957 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002958 ReservedSpace = 2+NumCases*2;
2959 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002960 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002961
Gabor Greif2d3024d2008-05-26 21:33:52 +00002962 OperandList[0] = Value;
2963 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002964}
2965
Chris Lattner2195fc42007-02-24 00:55:48 +00002966/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2967/// switch on and a default destination. The number of additional cases can
2968/// be specified here to make memory allocation more efficient. This
2969/// constructor can also autoinsert before another instruction.
2970SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2971 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002972 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2973 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002974 init(Value, Default, NumCases);
2975}
2976
2977/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2978/// switch on and a default destination. The number of additional cases can
2979/// be specified here to make memory allocation more efficient. This
2980/// constructor also autoinserts at the end of the specified BasicBlock.
2981SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2982 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002983 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2984 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002985 init(Value, Default, NumCases);
2986}
2987
Misha Brukmanb1c93172005-04-21 23:48:37 +00002988SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002989 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002990 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002991 Use *OL = OperandList, *InOL = SI.OperandList;
2992 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002993 OL[i] = InOL[i];
2994 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002995 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002996 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002997}
2998
Gordon Henriksen14a55692007-12-10 02:14:30 +00002999SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003000 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003001}
3002
3003
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003004/// addCase - Add an entry to the switch instruction...
3005///
Chris Lattner47ac1872005-02-24 05:32:09 +00003006void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003007 unsigned OpNo = NumOperands;
3008 if (OpNo+2 > ReservedSpace)
3009 resizeOperands(0); // Get more space!
3010 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003011 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003012 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003013 OperandList[OpNo] = OnVal;
3014 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003015}
3016
3017/// removeCase - This method removes the specified successor from the switch
3018/// instruction. Note that this cannot be used to remove the default
3019/// destination (successor #0).
3020///
3021void SwitchInst::removeCase(unsigned idx) {
3022 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003023 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3024
3025 unsigned NumOps = getNumOperands();
3026 Use *OL = OperandList;
3027
3028 // Move everything after this operand down.
3029 //
3030 // FIXME: we could just swap with the end of the list, then erase. However,
3031 // client might not expect this to happen. The code as it is thrashes the
3032 // use/def lists, which is kinda lame.
3033 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3034 OL[i-2] = OL[i];
3035 OL[i-2+1] = OL[i+1];
3036 }
3037
3038 // Nuke the last value.
3039 OL[NumOps-2].set(0);
3040 OL[NumOps-2+1].set(0);
3041 NumOperands = NumOps-2;
3042}
3043
3044/// resizeOperands - resize operands - This adjusts the length of the operands
3045/// list according to the following behavior:
3046/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003047/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003048/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3049/// 3. If NumOps == NumOperands, trim the reserved space.
3050///
3051void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003052 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003053 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003054 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003055 } else if (NumOps*2 > NumOperands) {
3056 // No resize needed.
3057 if (ReservedSpace >= NumOps) return;
3058 } else if (NumOps == NumOperands) {
3059 if (ReservedSpace == NumOps) return;
3060 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003061 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003062 }
3063
3064 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003065 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003066 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003067 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003068 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003069 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003070 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003071 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003072}
3073
3074
3075BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3076 return getSuccessor(idx);
3077}
3078unsigned SwitchInst::getNumSuccessorsV() const {
3079 return getNumSuccessors();
3080}
3081void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3082 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003083}
Chris Lattnerf22be932004-10-15 23:52:53 +00003084
Chris Lattner3ed871f2009-10-27 19:13:16 +00003085//===----------------------------------------------------------------------===//
3086// SwitchInst Implementation
3087//===----------------------------------------------------------------------===//
3088
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003089void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003090 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003091 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003092 ReservedSpace = 1+NumDests;
3093 NumOperands = 1;
3094 OperandList = allocHungoffUses(ReservedSpace);
3095
3096 OperandList[0] = Address;
3097}
3098
3099
3100/// resizeOperands - resize operands - This adjusts the length of the operands
3101/// list according to the following behavior:
3102/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3103/// of operation. This grows the number of ops by 2 times.
3104/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3105/// 3. If NumOps == NumOperands, trim the reserved space.
3106///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003107void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003108 unsigned e = getNumOperands();
3109 if (NumOps == 0) {
3110 NumOps = e*2;
3111 } else if (NumOps*2 > NumOperands) {
3112 // No resize needed.
3113 if (ReservedSpace >= NumOps) return;
3114 } else if (NumOps == NumOperands) {
3115 if (ReservedSpace == NumOps) return;
3116 } else {
3117 return;
3118 }
3119
3120 ReservedSpace = NumOps;
3121 Use *NewOps = allocHungoffUses(NumOps);
3122 Use *OldOps = OperandList;
3123 for (unsigned i = 0; i != e; ++i)
3124 NewOps[i] = OldOps[i];
3125 OperandList = NewOps;
3126 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3127}
3128
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003129IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3130 Instruction *InsertBefore)
3131: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003132 0, 0, InsertBefore) {
3133 init(Address, NumCases);
3134}
3135
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003136IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3137 BasicBlock *InsertAtEnd)
3138: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003139 0, 0, InsertAtEnd) {
3140 init(Address, NumCases);
3141}
3142
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003143IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3144 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003145 allocHungoffUses(IBI.getNumOperands()),
3146 IBI.getNumOperands()) {
3147 Use *OL = OperandList, *InOL = IBI.OperandList;
3148 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3149 OL[i] = InOL[i];
3150 SubclassOptionalData = IBI.SubclassOptionalData;
3151}
3152
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003153IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003154 dropHungoffUses(OperandList);
3155}
3156
3157/// addDestination - Add a destination.
3158///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003159void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003160 unsigned OpNo = NumOperands;
3161 if (OpNo+1 > ReservedSpace)
3162 resizeOperands(0); // Get more space!
3163 // Initialize some new operands.
3164 assert(OpNo < ReservedSpace && "Growing didn't work!");
3165 NumOperands = OpNo+1;
3166 OperandList[OpNo] = DestBB;
3167}
3168
3169/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003170/// indirectbr instruction.
3171void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003172 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3173
3174 unsigned NumOps = getNumOperands();
3175 Use *OL = OperandList;
3176
3177 // Replace this value with the last one.
3178 OL[idx+1] = OL[NumOps-1];
3179
3180 // Nuke the last value.
3181 OL[NumOps-1].set(0);
3182 NumOperands = NumOps-1;
3183}
3184
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003185BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003186 return getSuccessor(idx);
3187}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003188unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003189 return getNumSuccessors();
3190}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003191void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003192 setSuccessor(idx, B);
3193}
3194
3195//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003196// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003197//===----------------------------------------------------------------------===//
3198
Chris Lattnerf22be932004-10-15 23:52:53 +00003199// Define these methods here so vtables don't get emitted into every translation
3200// unit that uses these classes.
3201
Devang Patel11cf3f42009-10-27 22:16:29 +00003202GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3203 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003204}
3205
Devang Patel11cf3f42009-10-27 22:16:29 +00003206BinaryOperator *BinaryOperator::clone_impl() const {
3207 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003208}
3209
Devang Patel11cf3f42009-10-27 22:16:29 +00003210FCmpInst* FCmpInst::clone_impl() const {
3211 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003212}
3213
Devang Patel11cf3f42009-10-27 22:16:29 +00003214ICmpInst* ICmpInst::clone_impl() const {
3215 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003216}
3217
Devang Patel11cf3f42009-10-27 22:16:29 +00003218ExtractValueInst *ExtractValueInst::clone_impl() const {
3219 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003220}
3221
Devang Patel11cf3f42009-10-27 22:16:29 +00003222InsertValueInst *InsertValueInst::clone_impl() const {
3223 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003224}
3225
Devang Patel11cf3f42009-10-27 22:16:29 +00003226AllocaInst *AllocaInst::clone_impl() const {
3227 return new AllocaInst(getAllocatedType(),
3228 (Value*)getOperand(0),
3229 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003230}
3231
Devang Patel11cf3f42009-10-27 22:16:29 +00003232LoadInst *LoadInst::clone_impl() const {
3233 return new LoadInst(getOperand(0),
3234 Twine(), isVolatile(),
3235 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003236}
3237
Devang Patel11cf3f42009-10-27 22:16:29 +00003238StoreInst *StoreInst::clone_impl() const {
3239 return new StoreInst(getOperand(0), getOperand(1),
3240 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003241}
3242
Devang Patel11cf3f42009-10-27 22:16:29 +00003243TruncInst *TruncInst::clone_impl() const {
3244 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003245}
3246
Devang Patel11cf3f42009-10-27 22:16:29 +00003247ZExtInst *ZExtInst::clone_impl() const {
3248 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003249}
3250
Devang Patel11cf3f42009-10-27 22:16:29 +00003251SExtInst *SExtInst::clone_impl() const {
3252 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003253}
3254
Devang Patel11cf3f42009-10-27 22:16:29 +00003255FPTruncInst *FPTruncInst::clone_impl() const {
3256 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003257}
3258
Devang Patel11cf3f42009-10-27 22:16:29 +00003259FPExtInst *FPExtInst::clone_impl() const {
3260 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003261}
3262
Devang Patel11cf3f42009-10-27 22:16:29 +00003263UIToFPInst *UIToFPInst::clone_impl() const {
3264 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003265}
3266
Devang Patel11cf3f42009-10-27 22:16:29 +00003267SIToFPInst *SIToFPInst::clone_impl() const {
3268 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003269}
3270
Devang Patel11cf3f42009-10-27 22:16:29 +00003271FPToUIInst *FPToUIInst::clone_impl() const {
3272 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003273}
3274
Devang Patel11cf3f42009-10-27 22:16:29 +00003275FPToSIInst *FPToSIInst::clone_impl() const {
3276 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003277}
3278
Devang Patel11cf3f42009-10-27 22:16:29 +00003279PtrToIntInst *PtrToIntInst::clone_impl() const {
3280 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003281}
3282
Devang Patel11cf3f42009-10-27 22:16:29 +00003283IntToPtrInst *IntToPtrInst::clone_impl() const {
3284 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003285}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003286
Devang Patel11cf3f42009-10-27 22:16:29 +00003287BitCastInst *BitCastInst::clone_impl() const {
3288 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003289}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003290
Devang Patel11cf3f42009-10-27 22:16:29 +00003291CallInst *CallInst::clone_impl() const {
3292 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003293}
3294
Devang Patel11cf3f42009-10-27 22:16:29 +00003295SelectInst *SelectInst::clone_impl() const {
3296 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003297}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003298
Devang Patel11cf3f42009-10-27 22:16:29 +00003299VAArgInst *VAArgInst::clone_impl() const {
3300 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003301}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003302
Devang Patel11cf3f42009-10-27 22:16:29 +00003303ExtractElementInst *ExtractElementInst::clone_impl() const {
3304 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003305}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003306
Devang Patel11cf3f42009-10-27 22:16:29 +00003307InsertElementInst *InsertElementInst::clone_impl() const {
3308 return InsertElementInst::Create(getOperand(0),
3309 getOperand(1),
3310 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003311}
3312
Devang Patel11cf3f42009-10-27 22:16:29 +00003313ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3314 return new ShuffleVectorInst(getOperand(0),
3315 getOperand(1),
3316 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003317}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003318
Devang Patel11cf3f42009-10-27 22:16:29 +00003319PHINode *PHINode::clone_impl() const {
3320 return new PHINode(*this);
3321}
3322
3323ReturnInst *ReturnInst::clone_impl() const {
3324 return new(getNumOperands()) ReturnInst(*this);
3325}
3326
3327BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003328 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003329 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003330}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003331
Devang Patel11cf3f42009-10-27 22:16:29 +00003332SwitchInst *SwitchInst::clone_impl() const {
3333 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003334}
3335
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003336IndirectBrInst *IndirectBrInst::clone_impl() const {
3337 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003338}
3339
3340
Devang Patel11cf3f42009-10-27 22:16:29 +00003341InvokeInst *InvokeInst::clone_impl() const {
3342 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003343}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003344
Devang Patel11cf3f42009-10-27 22:16:29 +00003345UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003346 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003347 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003348}
3349
Devang Patel11cf3f42009-10-27 22:16:29 +00003350UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003351 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003352 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003353}