blob: 27bf23d296a75f95e5a49d0fa1cb4c9feb69a37c [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"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
65 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
66 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
69 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
70 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
Jay Foad61ea0e42011-06-23 09:09:15 +000090 std::copy(PN.op_begin(), PN.op_end(), op_begin());
91 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000092 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000093}
94
Gordon Henriksen14a55692007-12-10 02:14:30 +000095PHINode::~PHINode() {
Jay Foadbbb91f22011-01-16 15:30:52 +000096 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Jay Foad61ea0e42011-06-23 09:09:15 +000099Use *PHINode::allocHungoffUses(unsigned N) const {
100 // Allocate the array of Uses of the incoming values, followed by a pointer
101 // (with bottom bit set) to the User, followed by the array of pointers to
102 // the incoming basic blocks.
103 size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
104 + N * sizeof(BasicBlock*);
105 Use *Begin = static_cast<Use*>(::operator new(size));
106 Use *End = Begin + N;
107 (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1);
108 return Use::initTags(Begin, End);
109}
110
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111// removeIncomingValue - Remove an incoming value. This is useful if a
112// predecessor basic block is deleted.
113Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000114 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115
116 // Move everything after this operand down.
117 //
118 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000121 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
122 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123
124 // Nuke the last value.
Jay Foad61ea0e42011-06-23 09:09:15 +0000125 Op<-1>().set(0);
126 --NumOperands;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000127
128 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000129 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000131 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000132 eraseFromParent();
133 }
134 return Removed;
135}
136
Jay Foade98f29d2011-04-01 08:00:58 +0000137/// growOperands - grow operands - This grows the operand list in response
138/// to a push_back style of operation. This grows the number of ops by 1.5
139/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000140///
Jay Foade98f29d2011-04-01 08:00:58 +0000141void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000142 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000143 unsigned NumOps = e + e / 2;
144 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
145
146 Use *OldOps = op_begin();
147 BasicBlock **OldBlocks = block_begin();
Jay Foade03c05c2011-06-20 14:38:01 +0000148
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 ReservedSpace = NumOps;
Jay Foad61ea0e42011-06-23 09:09:15 +0000150 OperandList = allocHungoffUses(ReservedSpace);
151
152 std::copy(OldOps, OldOps + e, op_begin());
153 std::copy(OldBlocks, OldBlocks + e, block_begin());
154
Jay Foadbbb91f22011-01-16 15:30:52 +0000155 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156}
157
Nate Begemanb3923212005-08-04 23:24:19 +0000158/// hasConstantValue - If the specified PHI node always merges together the same
159/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000160Value *PHINode::hasConstantValue() const {
161 // Exploit the fact that phi nodes always have at least one entry.
162 Value *ConstantValue = getIncomingValue(0);
163 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
164 if (getIncomingValue(i) != ConstantValue)
165 return 0; // Incoming values not all the same.
166 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000167}
168
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000169
170//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000171// CallInst Implementation
172//===----------------------------------------------------------------------===//
173
Gordon Henriksen14a55692007-12-10 02:14:30 +0000174CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000175}
176
Chris Lattner054ba2c2007-02-13 00:58:44 +0000177void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000178 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000179 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000180
Misha Brukmanb1c93172005-04-21 23:48:37 +0000181 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000182 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000183 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000184
Chris Lattner054ba2c2007-02-13 00:58:44 +0000185 assert((NumParams == FTy->getNumParams() ||
186 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000187 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000188 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000189 assert((i >= FTy->getNumParams() ||
190 FTy->getParamType(i) == Params[i]->getType()) &&
191 "Calling a function with a bad signature!");
Gabor Greif6d673952010-07-16 09:38:02 +0000192 OperandList[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000193 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000194}
195
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000196void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000197 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000198 Op<-1>() = Func;
199 Op<0>() = Actual1;
200 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000201
202 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000204 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000205
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000206 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000207 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000208 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000209 assert((0 >= FTy->getNumParams() ||
210 FTy->getParamType(0) == Actual1->getType()) &&
211 "Calling a function with a bad signature!");
212 assert((1 >= FTy->getNumParams() ||
213 FTy->getParamType(1) == Actual2->getType()) &&
214 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000215}
216
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000217void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000218 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000219 Op<-1>() = Func;
220 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000221
222 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000223 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000224 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000225
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226 assert((FTy->getNumParams() == 1 ||
227 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000228 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000229 assert((0 == FTy->getNumParams() ||
230 FTy->getParamType(0) == Actual->getType()) &&
231 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232}
233
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000234void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000235 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000236 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000237
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000238 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000240 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000241
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000242 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000243}
244
Daniel Dunbar4975db62009-07-25 04:41:11 +0000245CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000246 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000247 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
248 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000249 Instruction::Call,
250 OperandTraits<CallInst>::op_end(this) - 2,
251 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000252 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000253 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254}
255
Daniel Dunbar4975db62009-07-25 04:41:11 +0000256CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257 BasicBlock *InsertAtEnd)
258 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
259 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000260 Instruction::Call,
261 OperandTraits<CallInst>::op_end(this) - 2,
262 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000264 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000266CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267 Instruction *InsertBefore)
268 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
269 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000270 Instruction::Call,
271 OperandTraits<CallInst>::op_end(this) - 1,
272 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000274 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275}
276
Daniel Dunbar4975db62009-07-25 04:41:11 +0000277CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278 BasicBlock *InsertAtEnd)
279 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
280 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000281 Instruction::Call,
282 OperandTraits<CallInst>::op_end(this) - 1,
283 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000285 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286}
287
Misha Brukmanb1c93172005-04-21 23:48:37 +0000288CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000289 : Instruction(CI.getType(), Instruction::Call,
290 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000291 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000292 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000293 setTailCall(CI.isTailCall());
294 setCallingConv(CI.getCallingConv());
295
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000296 Use *OL = OperandList;
297 Use *InOL = CI.OperandList;
298 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000299 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000300 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301}
302
Devang Patel4c758ea2008-09-25 21:00:45 +0000303void CallInst::addAttribute(unsigned i, Attributes attr) {
304 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000305 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000306 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000307}
308
Devang Patel4c758ea2008-09-25 21:00:45 +0000309void CallInst::removeAttribute(unsigned i, Attributes attr) {
310 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000311 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000312 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000313}
314
Devang Patelba3fa6c2008-09-23 23:03:40 +0000315bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000316 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000317 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000318 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000319 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000320 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000321}
322
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000323/// IsConstantOne - Return true only if val is constant int 1
324static bool IsConstantOne(Value *val) {
325 assert(val && "IsConstantOne does not work with NULL val");
326 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
327}
328
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000329static Instruction *createMalloc(Instruction *InsertBefore,
330 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000331 const Type *AllocTy, Value *AllocSize,
332 Value *ArraySize, Function *MallocF,
333 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000334 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000335 "createMalloc needs either InsertBefore or InsertAtEnd");
336
337 // malloc(type) becomes:
338 // bitcast (i8* malloc(typeSize)) to type*
339 // malloc(type, arraySize) becomes:
340 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000341 if (!ArraySize)
342 ArraySize = ConstantInt::get(IntPtrTy, 1);
343 else if (ArraySize->getType() != IntPtrTy) {
344 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000345 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
346 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000347 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000348 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
349 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000350 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000351
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000352 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000353 if (IsConstantOne(AllocSize)) {
354 AllocSize = ArraySize; // Operand * 1 = Operand
355 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
356 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
357 false /*ZExt*/);
358 // Malloc arg is constant product of type size and array size
359 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
360 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000361 // Multiply type size by the array size...
362 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000363 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
364 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000365 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000366 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
367 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000368 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000369 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000370
Victor Hernandez788eaab2009-09-18 19:20:02 +0000371 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000372 // Create the call to Malloc.
373 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
374 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000375 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000376 Value *MallocFunc = MallocF;
377 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000378 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000379 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000380 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000381 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000382 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000383 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000384 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000385 Result = MCall;
386 if (Result->getType() != AllocPtrType)
387 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000388 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000389 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000390 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000391 Result = MCall;
392 if (Result->getType() != AllocPtrType) {
393 InsertAtEnd->getInstList().push_back(MCall);
394 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000395 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000396 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000397 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000398 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000399 if (Function *F = dyn_cast<Function>(MallocFunc)) {
400 MCall->setCallingConv(F->getCallingConv());
401 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
402 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000403 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000404
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000405 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406}
407
408/// CreateMalloc - Generate the IR for a call to malloc:
409/// 1. Compute the malloc call's argument as the specified type's size,
410/// possibly multiplied by the array size if the array size is not
411/// constant 1.
412/// 2. Call malloc with that argument.
413/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000414Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
415 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000416 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000417 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000418 const Twine &Name) {
419 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000420 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000421}
422
423/// CreateMalloc - Generate the IR for a call to malloc:
424/// 1. Compute the malloc call's argument as the specified type's size,
425/// possibly multiplied by the array size if the array size is not
426/// constant 1.
427/// 2. Call malloc with that argument.
428/// 3. Bitcast the result of the malloc call to the specified type.
429/// Note: This function does not add the bitcast to the basic block, that is the
430/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000431Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
432 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000433 Value *AllocSize, Value *ArraySize,
434 Function *MallocF, const Twine &Name) {
435 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000436 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000437}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000438
Victor Hernandeze2971492009-10-24 04:23:03 +0000439static Instruction* createFree(Value* Source, Instruction *InsertBefore,
440 BasicBlock *InsertAtEnd) {
441 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
442 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000443 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000444 "Can not free something of nonpointer type!");
445
446 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
447 Module* M = BB->getParent()->getParent();
448
449 const Type *VoidTy = Type::getVoidTy(M->getContext());
450 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
451 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000452 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000453 CallInst* Result = NULL;
454 Value *PtrCast = Source;
455 if (InsertBefore) {
456 if (Source->getType() != IntPtrTy)
457 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
458 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
459 } else {
460 if (Source->getType() != IntPtrTy)
461 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
462 Result = CallInst::Create(FreeFunc, PtrCast, "");
463 }
464 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000465 if (Function *F = dyn_cast<Function>(FreeFunc))
466 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000467
468 return Result;
469}
470
471/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000472Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
473 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000474}
475
476/// CreateFree - Generate the IR for a call to the builtin free function.
477/// Note: This function does not add the call to the basic block, that is the
478/// responsibility of the caller.
479Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
480 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
481 assert(FreeCall && "CreateFree did not create a CallInst");
482 return FreeCall;
483}
484
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000485//===----------------------------------------------------------------------===//
486// InvokeInst Implementation
487//===----------------------------------------------------------------------===//
488
489void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000490 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000491 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000492 Op<-3>() = Fn;
493 Op<-2>() = IfNormal;
494 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000495 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000496 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000497 (void)FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000498
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000499 assert(((NumArgs == FTy->getNumParams()) ||
500 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000501 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000502
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000503 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000504 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000505 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000506 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000507 "Invoking a function with a bad signature!");
508
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000509 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000510 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000511}
512
Misha Brukmanb1c93172005-04-21 23:48:37 +0000513InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000514 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000515 OperandTraits<InvokeInst>::op_end(this)
516 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000517 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000518 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000519 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000520 Use *OL = OperandList, *InOL = II.OperandList;
521 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000522 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000523 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000524}
525
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000526BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
527 return getSuccessor(idx);
528}
529unsigned InvokeInst::getNumSuccessorsV() const {
530 return getNumSuccessors();
531}
532void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
533 return setSuccessor(idx, B);
534}
535
Devang Patelba3fa6c2008-09-23 23:03:40 +0000536bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000537 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000538 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000539 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000540 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000541 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000542}
543
Devang Patel4c758ea2008-09-25 21:00:45 +0000544void InvokeInst::addAttribute(unsigned i, Attributes attr) {
545 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000546 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000547 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000548}
549
Devang Patel4c758ea2008-09-25 21:00:45 +0000550void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
551 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000552 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000553 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000554}
555
Duncan Sands5208d1a2007-11-28 17:07:01 +0000556
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000557//===----------------------------------------------------------------------===//
558// ReturnInst Implementation
559//===----------------------------------------------------------------------===//
560
Chris Lattner2195fc42007-02-24 00:55:48 +0000561ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000562 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000563 OperandTraits<ReturnInst>::op_end(this) -
564 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000565 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000566 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000567 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000568 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000569}
570
Owen Anderson55f1c092009-08-13 21:58:54 +0000571ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
572 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000573 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
574 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000575 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000576 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000577}
Owen Anderson55f1c092009-08-13 21:58:54 +0000578ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
579 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000580 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
581 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000582 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000583 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000584}
Owen Anderson55f1c092009-08-13 21:58:54 +0000585ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
586 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000587 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000588}
589
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590unsigned ReturnInst::getNumSuccessorsV() const {
591 return getNumSuccessors();
592}
593
Devang Patelae682fb2008-02-26 17:56:20 +0000594/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
595/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000597 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000598}
599
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000600BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000601 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602 return 0;
603}
604
Devang Patel59643e52008-02-23 00:35:18 +0000605ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000606}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000607
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000608//===----------------------------------------------------------------------===//
609// UnwindInst Implementation
610//===----------------------------------------------------------------------===//
611
Owen Anderson55f1c092009-08-13 21:58:54 +0000612UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
613 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
614 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000615}
Owen Anderson55f1c092009-08-13 21:58:54 +0000616UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
617 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
618 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000619}
620
621
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000622unsigned UnwindInst::getNumSuccessorsV() const {
623 return getNumSuccessors();
624}
625
626void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000627 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000628}
629
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000630BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000631 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632 return 0;
633}
634
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000635//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000636// UnreachableInst Implementation
637//===----------------------------------------------------------------------===//
638
Owen Anderson55f1c092009-08-13 21:58:54 +0000639UnreachableInst::UnreachableInst(LLVMContext &Context,
640 Instruction *InsertBefore)
641 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
642 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000643}
Owen Anderson55f1c092009-08-13 21:58:54 +0000644UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
645 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
646 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000647}
648
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000649unsigned UnreachableInst::getNumSuccessorsV() const {
650 return getNumSuccessors();
651}
652
653void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000654 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655}
656
657BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000658 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000660}
661
662//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000663// BranchInst Implementation
664//===----------------------------------------------------------------------===//
665
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000666void BranchInst::AssertOK() {
667 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000668 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000669 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000670}
671
Chris Lattner2195fc42007-02-24 00:55:48 +0000672BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000673 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000674 OperandTraits<BranchInst>::op_end(this) - 1,
675 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000676 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000677 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000678}
679BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
680 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000681 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000682 OperandTraits<BranchInst>::op_end(this) - 3,
683 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000684 Op<-1>() = IfTrue;
685 Op<-2>() = IfFalse;
686 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000687#ifndef NDEBUG
688 AssertOK();
689#endif
690}
691
692BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000693 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000694 OperandTraits<BranchInst>::op_end(this) - 1,
695 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000696 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000697 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000698}
699
700BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
701 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000702 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000703 OperandTraits<BranchInst>::op_end(this) - 3,
704 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000705 Op<-1>() = IfTrue;
706 Op<-2>() = IfFalse;
707 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000708#ifndef NDEBUG
709 AssertOK();
710#endif
711}
712
713
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000714BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000715 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000716 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
717 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000718 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000719 if (BI.getNumOperands() != 1) {
720 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000721 Op<-3>() = BI.Op<-3>();
722 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000724 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
728 return getSuccessor(idx);
729}
730unsigned BranchInst::getNumSuccessorsV() const {
731 return getNumSuccessors();
732}
733void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
734 setSuccessor(idx, B);
735}
736
737
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000738//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000739// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000740//===----------------------------------------------------------------------===//
741
Owen Andersonb6b25302009-07-14 23:09:55 +0000742static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000743 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000744 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000745 else {
746 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000747 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000748 assert(Amt->getType()->isIntegerTy() &&
749 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000750 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000751 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000752}
753
Victor Hernandez8acf2952009-10-23 21:09:37 +0000754AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
755 const Twine &Name, Instruction *InsertBefore)
756 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
757 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
758 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000759 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000760 setName(Name);
761}
762
763AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
764 const Twine &Name, BasicBlock *InsertAtEnd)
765 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
766 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
767 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000768 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000769 setName(Name);
770}
771
772AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
773 Instruction *InsertBefore)
774 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
775 getAISize(Ty->getContext(), 0), InsertBefore) {
776 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000777 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000778 setName(Name);
779}
780
781AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
782 BasicBlock *InsertAtEnd)
783 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
784 getAISize(Ty->getContext(), 0), InsertAtEnd) {
785 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000786 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000787 setName(Name);
788}
789
790AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
791 const Twine &Name, Instruction *InsertBefore)
792 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000793 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000794 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000795 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000796 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797}
798
Victor Hernandez8acf2952009-10-23 21:09:37 +0000799AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
800 const Twine &Name, BasicBlock *InsertAtEnd)
801 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000802 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000803 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000804 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000805 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000806}
807
Gordon Henriksen14a55692007-12-10 02:14:30 +0000808// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000809AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000810}
811
Victor Hernandez8acf2952009-10-23 21:09:37 +0000812void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000813 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000814 assert(Align <= MaximumAlignment &&
815 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000816 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000817 assert(getAlignment() == Align && "Alignment representation error!");
818}
819
Victor Hernandez8acf2952009-10-23 21:09:37 +0000820bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000821 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000822 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000823 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000824}
825
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000826Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000827 return getType()->getElementType();
828}
829
Chris Lattner8b291e62008-11-26 02:54:17 +0000830/// isStaticAlloca - Return true if this alloca is in the entry block of the
831/// function and is a constant size. If so, the code generator will fold it
832/// into the prolog/epilog code, so it is basically free.
833bool AllocaInst::isStaticAlloca() const {
834 // Must be constant size.
835 if (!isa<ConstantInt>(getArraySize())) return false;
836
837 // Must be in the entry block.
838 const BasicBlock *Parent = getParent();
839 return Parent == &Parent->getParent()->front();
840}
841
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000842//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000843// LoadInst Implementation
844//===----------------------------------------------------------------------===//
845
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000846void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000847 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000848 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000849}
850
Daniel Dunbar4975db62009-07-25 04:41:11 +0000851LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000852 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000853 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000854 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000855 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000856 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000857 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000858}
859
Daniel Dunbar4975db62009-07-25 04:41:11 +0000860LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000861 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000862 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000863 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000864 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000865 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000866 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000867}
868
Daniel Dunbar4975db62009-07-25 04:41:11 +0000869LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000870 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000871 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000873 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000874 setAlignment(0);
875 AssertOK();
876 setName(Name);
877}
878
Daniel Dunbar4975db62009-07-25 04:41:11 +0000879LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000880 unsigned Align, Instruction *InsertBef)
881 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
882 Load, Ptr, InsertBef) {
883 setVolatile(isVolatile);
884 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000885 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000886 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887}
888
Daniel Dunbar4975db62009-07-25 04:41:11 +0000889LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000890 unsigned Align, BasicBlock *InsertAE)
891 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
892 Load, Ptr, InsertAE) {
893 setVolatile(isVolatile);
894 setAlignment(Align);
895 AssertOK();
896 setName(Name);
897}
898
Daniel Dunbar4975db62009-07-25 04:41:11 +0000899LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000901 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000902 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000903 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000904 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000905 AssertOK();
906 setName(Name);
907}
908
Daniel Dunbar27096822009-08-11 18:11:15 +0000909
910
911LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
912 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
913 Load, Ptr, InsertBef) {
914 setVolatile(false);
915 setAlignment(0);
916 AssertOK();
917 if (Name && Name[0]) setName(Name);
918}
919
920LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
921 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
922 Load, Ptr, InsertAE) {
923 setVolatile(false);
924 setAlignment(0);
925 AssertOK();
926 if (Name && Name[0]) setName(Name);
927}
928
929LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
930 Instruction *InsertBef)
931: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
932 Load, Ptr, InsertBef) {
933 setVolatile(isVolatile);
934 setAlignment(0);
935 AssertOK();
936 if (Name && Name[0]) setName(Name);
937}
938
939LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
940 BasicBlock *InsertAE)
941 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
942 Load, Ptr, InsertAE) {
943 setVolatile(isVolatile);
944 setAlignment(0);
945 AssertOK();
946 if (Name && Name[0]) setName(Name);
947}
948
Christopher Lamb84485702007-04-22 19:24:39 +0000949void LoadInst::setAlignment(unsigned Align) {
950 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000951 assert(Align <= MaximumAlignment &&
952 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000953 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
954 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000955 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000956}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000957
958//===----------------------------------------------------------------------===//
959// StoreInst Implementation
960//===----------------------------------------------------------------------===//
961
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000962void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000963 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000964 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965 "Ptr must have pointer type!");
966 assert(getOperand(0)->getType() ==
967 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000968 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000969}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000970
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000971
972StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000973 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000974 OperandTraits<StoreInst>::op_begin(this),
975 OperandTraits<StoreInst>::operands(this),
976 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000977 Op<0>() = val;
978 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000979 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000980 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000981 AssertOK();
982}
983
984StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000985 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000986 OperandTraits<StoreInst>::op_begin(this),
987 OperandTraits<StoreInst>::operands(this),
988 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000989 Op<0>() = val;
990 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000991 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000992 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993 AssertOK();
994}
995
Misha Brukmanb1c93172005-04-21 23:48:37 +0000996StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000997 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000998 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000999 OperandTraits<StoreInst>::op_begin(this),
1000 OperandTraits<StoreInst>::operands(this),
1001 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001002 Op<0>() = val;
1003 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001004 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001005 setAlignment(0);
1006 AssertOK();
1007}
1008
1009StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1010 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001011 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001012 OperandTraits<StoreInst>::op_begin(this),
1013 OperandTraits<StoreInst>::operands(this),
1014 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001015 Op<0>() = val;
1016 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001017 setVolatile(isVolatile);
1018 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001019 AssertOK();
1020}
1021
Misha Brukmanb1c93172005-04-21 23:48:37 +00001022StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001023 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001024 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001025 OperandTraits<StoreInst>::op_begin(this),
1026 OperandTraits<StoreInst>::operands(this),
1027 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001028 Op<0>() = val;
1029 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001030 setVolatile(isVolatile);
1031 setAlignment(Align);
1032 AssertOK();
1033}
1034
1035StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001036 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001037 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001038 OperandTraits<StoreInst>::op_begin(this),
1039 OperandTraits<StoreInst>::operands(this),
1040 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001041 Op<0>() = val;
1042 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001043 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001044 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001045 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001046}
1047
Christopher Lamb84485702007-04-22 19:24:39 +00001048void StoreInst::setAlignment(unsigned Align) {
1049 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001050 assert(Align <= MaximumAlignment &&
1051 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001052 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1053 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001054 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001055}
1056
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001057//===----------------------------------------------------------------------===//
1058// GetElementPtrInst Implementation
1059//===----------------------------------------------------------------------===//
1060
Christopher Lambedf07882007-12-17 01:12:55 +00001061static unsigned retrieveAddrSpace(const Value *Val) {
1062 return cast<PointerType>(Val->getType())->getAddressSpace();
1063}
1064
Gabor Greif21ba1842008-06-06 20:28:12 +00001065void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001066 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001067 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1068 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001069 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001070
Chris Lattner79807c3d2007-01-31 19:47:18 +00001071 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001072 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001073
1074 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001075}
1076
Daniel Dunbar4975db62009-07-25 04:41:11 +00001077void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001078 assert(NumOperands == 2 && "NumOperands not initialized?");
1079 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001080 OL[0] = Ptr;
1081 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001082
1083 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001084}
1085
Gabor Greiff6caff662008-05-10 08:32:32 +00001086GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001087 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001088 OperandTraits<GetElementPtrInst>::op_end(this)
1089 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001090 GEPI.getNumOperands()) {
1091 Use *OL = OperandList;
1092 Use *GEPIOL = GEPI.OperandList;
1093 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001094 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001095 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001096}
1097
Chris Lattner82981202005-05-03 05:43:30 +00001098GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001099 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001100 : Instruction(PointerType::get(
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001101 checkGEPType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001102 GetElementPtr,
1103 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1104 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001105 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001106}
1107
1108GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001109 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001110 : Instruction(PointerType::get(
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001111 checkGEPType(getIndexedType(Ptr->getType(),Idx)),
Owen Andersond420fd42009-07-16 00:03:07 +00001112 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001113 GetElementPtr,
1114 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1115 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001116 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001117}
1118
Chris Lattner6090a422009-03-09 04:46:40 +00001119/// getIndexedType - Returns the type of the element that would be accessed with
1120/// a gep instruction with the specified parameters.
1121///
1122/// The Idxs pointer should point to a continuous piece of memory containing the
1123/// indices, either as Value* or uint64_t.
1124///
1125/// A null type is returned if the indices are invalid for the specified
1126/// pointer type.
1127///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001128template <typename IndexTy>
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001129static Type *getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1130 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001131 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1132 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001133 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001134
Chris Lattner6090a422009-03-09 04:46:40 +00001135 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001136 if (NumIdx == 0)
1137 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001138
1139 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001140 // it cannot be 'stepped over'.
1141 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001142 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001143
Dan Gohman1ecaf452008-05-31 00:58:22 +00001144 unsigned CurIdx = 1;
1145 for (; CurIdx != NumIdx; ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001146 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001147 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001148 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001149 if (!CT->indexValid(Index)) return 0;
1150 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001151 }
1152 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001153}
1154
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001155Type *GetElementPtrInst::getIndexedType(const Type *Ptr, Value* const *Idxs,
1156 unsigned NumIdx) {
Matthijs Kooijman04468622008-07-29 08:46:11 +00001157 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1158}
1159
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001160Type *GetElementPtrInst::getIndexedType(const Type *Ptr,
1161 Constant* const *Idxs,
1162 unsigned NumIdx) {
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001163 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1164}
1165
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001166Type *GetElementPtrInst::getIndexedType(const Type *Ptr,
1167 uint64_t const *Idxs,
1168 unsigned NumIdx) {
Matthijs Kooijman04468622008-07-29 08:46:11 +00001169 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1170}
1171
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001172Type *GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
Chris Lattner82981202005-05-03 05:43:30 +00001173 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1174 if (!PTy) return 0; // Type isn't a pointer type!
1175
1176 // Check the pointer index.
1177 if (!PTy->indexValid(Idx)) return 0;
1178
Chris Lattnerc2233332005-05-03 16:44:45 +00001179 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001180}
1181
Chris Lattner45f15572007-04-14 00:12:57 +00001182
1183/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1184/// zeros. If so, the result pointer and the first operand have the same
1185/// value, just potentially different types.
1186bool GetElementPtrInst::hasAllZeroIndices() const {
1187 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1188 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1189 if (!CI->isZero()) return false;
1190 } else {
1191 return false;
1192 }
1193 }
1194 return true;
1195}
1196
Chris Lattner27058292007-04-27 20:35:56 +00001197/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1198/// constant integers. If so, the result pointer and the first operand have
1199/// a constant offset between them.
1200bool GetElementPtrInst::hasAllConstantIndices() const {
1201 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1202 if (!isa<ConstantInt>(getOperand(i)))
1203 return false;
1204 }
1205 return true;
1206}
1207
Dan Gohman1b849082009-09-07 23:54:19 +00001208void GetElementPtrInst::setIsInBounds(bool B) {
1209 cast<GEPOperator>(this)->setIsInBounds(B);
1210}
Chris Lattner45f15572007-04-14 00:12:57 +00001211
Nick Lewycky28a5f252009-09-27 21:33:04 +00001212bool GetElementPtrInst::isInBounds() const {
1213 return cast<GEPOperator>(this)->isInBounds();
1214}
1215
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001216//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001217// ExtractElementInst Implementation
1218//===----------------------------------------------------------------------===//
1219
1220ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001221 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001222 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001223 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001224 ExtractElement,
1225 OperandTraits<ExtractElementInst>::op_begin(this),
1226 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001227 assert(isValidOperands(Val, Index) &&
1228 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001229 Op<0>() = Val;
1230 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001231 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001232}
1233
1234ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001235 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001236 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001237 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001238 ExtractElement,
1239 OperandTraits<ExtractElementInst>::op_begin(this),
1240 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001241 assert(isValidOperands(Val, Index) &&
1242 "Invalid extractelement instruction operands!");
1243
Gabor Greif2d3024d2008-05-26 21:33:52 +00001244 Op<0>() = Val;
1245 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001246 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001247}
1248
Chris Lattner65511ff2006-10-05 06:24:58 +00001249
Chris Lattner54865b32006-04-08 04:05:48 +00001250bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001251 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001252 return false;
1253 return true;
1254}
1255
1256
Robert Bocchino23004482006-01-10 19:05:34 +00001257//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001258// InsertElementInst Implementation
1259//===----------------------------------------------------------------------===//
1260
Chris Lattner54865b32006-04-08 04:05:48 +00001261InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001262 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001263 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001264 : Instruction(Vec->getType(), InsertElement,
1265 OperandTraits<InsertElementInst>::op_begin(this),
1266 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001267 assert(isValidOperands(Vec, Elt, Index) &&
1268 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001269 Op<0>() = Vec;
1270 Op<1>() = Elt;
1271 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001272 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001273}
1274
Chris Lattner54865b32006-04-08 04:05:48 +00001275InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001276 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001277 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001278 : Instruction(Vec->getType(), InsertElement,
1279 OperandTraits<InsertElementInst>::op_begin(this),
1280 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001281 assert(isValidOperands(Vec, Elt, Index) &&
1282 "Invalid insertelement instruction operands!");
1283
Gabor Greif2d3024d2008-05-26 21:33:52 +00001284 Op<0>() = Vec;
1285 Op<1>() = Elt;
1286 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001287 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001288}
1289
Chris Lattner54865b32006-04-08 04:05:48 +00001290bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1291 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001292 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001293 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001294
Reid Spencerd84d35b2007-02-15 02:26:10 +00001295 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001296 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001297
Duncan Sands9dff9be2010-02-15 16:12:20 +00001298 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001299 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001300 return true;
1301}
1302
1303
Robert Bocchinoca27f032006-01-17 20:07:22 +00001304//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001305// ShuffleVectorInst Implementation
1306//===----------------------------------------------------------------------===//
1307
1308ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001309 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001310 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001311: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001312 cast<VectorType>(Mask->getType())->getNumElements()),
1313 ShuffleVector,
1314 OperandTraits<ShuffleVectorInst>::op_begin(this),
1315 OperandTraits<ShuffleVectorInst>::operands(this),
1316 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001317 assert(isValidOperands(V1, V2, Mask) &&
1318 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001319 Op<0>() = V1;
1320 Op<1>() = V2;
1321 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001322 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001323}
1324
1325ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001326 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001327 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001328: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1329 cast<VectorType>(Mask->getType())->getNumElements()),
1330 ShuffleVector,
1331 OperandTraits<ShuffleVectorInst>::op_begin(this),
1332 OperandTraits<ShuffleVectorInst>::operands(this),
1333 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001334 assert(isValidOperands(V1, V2, Mask) &&
1335 "Invalid shuffle vector instruction operands!");
1336
Gabor Greif2d3024d2008-05-26 21:33:52 +00001337 Op<0>() = V1;
1338 Op<1>() = V2;
1339 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001340 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001341}
1342
Mon P Wang25f01062008-11-10 04:46:22 +00001343bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001344 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001345 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001346 return false;
1347
1348 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001349 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001350 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001351
1352 // Check to see if Mask is valid.
1353 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1354 const VectorType *VTy = cast<VectorType>(V1->getType());
1355 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1356 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1357 if (CI->uge(VTy->getNumElements()*2))
1358 return false;
1359 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1360 return false;
1361 }
1362 }
1363 }
1364 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1365 return false;
1366
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001367 return true;
1368}
1369
Chris Lattnerf724e342008-03-02 05:28:33 +00001370/// getMaskValue - Return the index from the shuffle mask for the specified
1371/// output result. This is either -1 if the element is undef or a number less
1372/// than 2*numelements.
1373int ShuffleVectorInst::getMaskValue(unsigned i) const {
1374 const Constant *Mask = cast<Constant>(getOperand(2));
1375 if (isa<UndefValue>(Mask)) return -1;
1376 if (isa<ConstantAggregateZero>(Mask)) return 0;
1377 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1378 assert(i < MaskCV->getNumOperands() && "Index out of range");
1379
1380 if (isa<UndefValue>(MaskCV->getOperand(i)))
1381 return -1;
1382 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1383}
1384
Dan Gohman12fce772008-05-15 19:50:34 +00001385//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001386// InsertValueInst Class
1387//===----------------------------------------------------------------------===//
1388
Jay Foad57aa6362011-07-13 10:26:04 +00001389void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1390 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001391 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001392
1393 // There's no fundamental reason why we require at least one index
1394 // (other than weirdness with &*IdxBegin being invalid; see
1395 // getelementptr's init routine for example). But there's no
1396 // present need to support it.
1397 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1398
1399 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001400 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001401 Op<0>() = Agg;
1402 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001403
Jay Foad57aa6362011-07-13 10:26:04 +00001404 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001405 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001406}
1407
1408InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001409 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001410 OperandTraits<InsertValueInst>::op_begin(this), 2),
1411 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001412 Op<0>() = IVI.getOperand(0);
1413 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001414 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001415}
1416
1417//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001418// ExtractValueInst Class
1419//===----------------------------------------------------------------------===//
1420
Jay Foad57aa6362011-07-13 10:26:04 +00001421void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001422 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001423
Jay Foad57aa6362011-07-13 10:26:04 +00001424 // There's no fundamental reason why we require at least one index.
1425 // But there's no present need to support it.
1426 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001427
Jay Foad57aa6362011-07-13 10:26:04 +00001428 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001429 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001430}
1431
1432ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001433 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001434 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001435 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001436}
1437
Dan Gohman12fce772008-05-15 19:50:34 +00001438// getIndexedType - Returns the type of the element that would be extracted
1439// with an extractvalue instruction with the specified parameters.
1440//
1441// A null type is returned if the indices are invalid for the specified
1442// pointer type.
1443//
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001444Type *ExtractValueInst::getIndexedType(const Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001445 ArrayRef<unsigned> Idxs) {
1446 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001447 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001448 // We can't use CompositeType::indexValid(Index) here.
1449 // indexValid() always returns true for arrays because getelementptr allows
1450 // out-of-bounds indices. Since we don't allow those for extractvalue and
1451 // insertvalue we need to check array indexing manually.
1452 // Since the only other types we can index into are struct types it's just
1453 // as easy to check those manually as well.
1454 if (const ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
1455 if (Index >= AT->getNumElements())
1456 return 0;
1457 } else if (const StructType *ST = dyn_cast<StructType>(Agg)) {
1458 if (Index >= ST->getNumElements())
1459 return 0;
1460 } else {
1461 // Not a valid type to index into.
1462 return 0;
1463 }
1464
1465 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001466 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001467 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001468}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001469
1470//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001471// BinaryOperator Class
1472//===----------------------------------------------------------------------===//
1473
Chris Lattner2195fc42007-02-24 00:55:48 +00001474BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001475 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001476 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001477 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001478 OperandTraits<BinaryOperator>::op_begin(this),
1479 OperandTraits<BinaryOperator>::operands(this),
1480 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001481 Op<0>() = S1;
1482 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001483 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001484 setName(Name);
1485}
1486
1487BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001488 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001489 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001490 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001491 OperandTraits<BinaryOperator>::op_begin(this),
1492 OperandTraits<BinaryOperator>::operands(this),
1493 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001494 Op<0>() = S1;
1495 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001496 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001497 setName(Name);
1498}
1499
1500
1501void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001502 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001503 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001504 assert(LHS->getType() == RHS->getType() &&
1505 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001506#ifndef NDEBUG
1507 switch (iType) {
1508 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001509 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001510 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001511 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001512 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001513 "Tried to create an integer operation on a non-integer type!");
1514 break;
1515 case FAdd: case FSub:
1516 case FMul:
1517 assert(getType() == LHS->getType() &&
1518 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001519 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001520 "Tried to create a floating-point operation on a "
1521 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001522 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001523 case UDiv:
1524 case SDiv:
1525 assert(getType() == LHS->getType() &&
1526 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001527 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001528 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001529 "Incorrect operand type (not integer) for S/UDIV");
1530 break;
1531 case FDiv:
1532 assert(getType() == LHS->getType() &&
1533 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001534 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001535 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001536 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001537 case URem:
1538 case SRem:
1539 assert(getType() == LHS->getType() &&
1540 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001541 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001542 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001543 "Incorrect operand type (not integer) for S/UREM");
1544 break;
1545 case FRem:
1546 assert(getType() == LHS->getType() &&
1547 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001548 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001549 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001550 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001551 case Shl:
1552 case LShr:
1553 case AShr:
1554 assert(getType() == LHS->getType() &&
1555 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001556 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001557 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001558 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001559 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001560 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001561 case And: case Or:
1562 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001563 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001564 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001565 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001566 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001567 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001568 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001570 default:
1571 break;
1572 }
1573#endif
1574}
1575
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001576BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001577 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001578 Instruction *InsertBefore) {
1579 assert(S1->getType() == S2->getType() &&
1580 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001581 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001582}
1583
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001584BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001585 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001586 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001587 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001588 InsertAtEnd->getInstList().push_back(Res);
1589 return Res;
1590}
1591
Dan Gohman5476cfd2009-08-12 16:23:25 +00001592BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001593 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001594 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001595 return new BinaryOperator(Instruction::Sub,
1596 zero, Op,
1597 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001598}
1599
Dan Gohman5476cfd2009-08-12 16:23:25 +00001600BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001601 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001602 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001603 return new BinaryOperator(Instruction::Sub,
1604 zero, Op,
1605 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001606}
1607
Dan Gohman4ab44202009-12-18 02:58:50 +00001608BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1609 Instruction *InsertBefore) {
1610 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1611 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1612}
1613
1614BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1615 BasicBlock *InsertAtEnd) {
1616 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1617 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1618}
1619
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001620BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1621 Instruction *InsertBefore) {
1622 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1623 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1624}
1625
1626BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1627 BasicBlock *InsertAtEnd) {
1628 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1629 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1630}
1631
Dan Gohman5476cfd2009-08-12 16:23:25 +00001632BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001633 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001634 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001635 return new BinaryOperator(Instruction::FSub,
1636 zero, Op,
1637 Op->getType(), Name, InsertBefore);
1638}
1639
Dan Gohman5476cfd2009-08-12 16:23:25 +00001640BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001641 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001642 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001643 return new BinaryOperator(Instruction::FSub,
1644 zero, Op,
1645 Op->getType(), Name, InsertAtEnd);
1646}
1647
Dan Gohman5476cfd2009-08-12 16:23:25 +00001648BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001649 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001650 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001651 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001652 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001653 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001654 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001655 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001656 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001657 }
1658
1659 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001660 Op->getType(), Name, InsertBefore);
1661}
1662
Dan Gohman5476cfd2009-08-12 16:23:25 +00001663BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001664 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001665 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001666 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001667 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001668 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001669 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001670 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001671 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001672 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001673 }
1674
1675 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676 Op->getType(), Name, InsertAtEnd);
1677}
1678
1679
1680// isConstantAllOnes - Helper function for several functions below
1681static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001682 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1683 return CI->isAllOnesValue();
1684 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1685 return CV->isAllOnesValue();
1686 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001687}
1688
Owen Andersonbb2501b2009-07-13 22:18:28 +00001689bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001690 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1691 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001692 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1693 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001694 return false;
1695}
1696
Owen Andersonbb2501b2009-07-13 22:18:28 +00001697bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001698 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1699 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001700 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001701 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001702 return false;
1703}
1704
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001705bool BinaryOperator::isNot(const Value *V) {
1706 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1707 return (Bop->getOpcode() == Instruction::Xor &&
1708 (isConstantAllOnes(Bop->getOperand(1)) ||
1709 isConstantAllOnes(Bop->getOperand(0))));
1710 return false;
1711}
1712
Chris Lattner2c7d1772005-04-24 07:28:37 +00001713Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001714 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001715}
1716
Chris Lattner2c7d1772005-04-24 07:28:37 +00001717const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1718 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001719}
1720
Dan Gohmana5b96452009-06-04 22:49:04 +00001721Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001722 return cast<BinaryOperator>(BinOp)->getOperand(1);
1723}
1724
1725const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1726 return getFNegArgument(const_cast<Value*>(BinOp));
1727}
1728
Chris Lattner2c7d1772005-04-24 07:28:37 +00001729Value *BinaryOperator::getNotArgument(Value *BinOp) {
1730 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1731 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1732 Value *Op0 = BO->getOperand(0);
1733 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001734 if (isConstantAllOnes(Op0)) return Op1;
1735
1736 assert(isConstantAllOnes(Op1));
1737 return Op0;
1738}
1739
Chris Lattner2c7d1772005-04-24 07:28:37 +00001740const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1741 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001742}
1743
1744
1745// swapOperands - Exchange the two operands to this instruction. This
1746// instruction is safe to use on any binary instruction and does not
1747// modify the semantics of the instruction. If the instruction is
1748// order dependent (SetLT f.e.) the opcode is changed.
1749//
1750bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001751 if (!isCommutative())
1752 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001753 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001754 return false;
1755}
1756
Dan Gohman1b849082009-09-07 23:54:19 +00001757void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1758 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1759}
1760
1761void BinaryOperator::setHasNoSignedWrap(bool b) {
1762 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1763}
1764
1765void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001766 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001767}
1768
Nick Lewycky28a5f252009-09-27 21:33:04 +00001769bool BinaryOperator::hasNoUnsignedWrap() const {
1770 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1771}
1772
1773bool BinaryOperator::hasNoSignedWrap() const {
1774 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1775}
1776
1777bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001778 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001779}
1780
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001781//===----------------------------------------------------------------------===//
1782// CastInst Class
1783//===----------------------------------------------------------------------===//
1784
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001785// Just determine if this cast only deals with integral->integral conversion.
1786bool CastInst::isIntegerCast() const {
1787 switch (getOpcode()) {
1788 default: return false;
1789 case Instruction::ZExt:
1790 case Instruction::SExt:
1791 case Instruction::Trunc:
1792 return true;
1793 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001794 return getOperand(0)->getType()->isIntegerTy() &&
1795 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001796 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001797}
1798
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001799bool CastInst::isLosslessCast() const {
1800 // Only BitCast can be lossless, exit fast if we're not BitCast
1801 if (getOpcode() != Instruction::BitCast)
1802 return false;
1803
1804 // Identity cast is always lossless
1805 const Type* SrcTy = getOperand(0)->getType();
1806 const Type* DstTy = getType();
1807 if (SrcTy == DstTy)
1808 return true;
1809
Reid Spencer8d9336d2006-12-31 05:26:44 +00001810 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001811 if (SrcTy->isPointerTy())
1812 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001813 return false; // Other types have no identity values
1814}
1815
1816/// This function determines if the CastInst does not require any bits to be
1817/// changed in order to effect the cast. Essentially, it identifies cases where
1818/// no code gen is necessary for the cast, hence the name no-op cast. For
1819/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001820/// # bitcast i32* %x to i8*
1821/// # bitcast <2 x i32> %x to <4 x i16>
1822/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001823/// @brief Determine if the described cast is a no-op.
1824bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1825 const Type *SrcTy,
1826 const Type *DestTy,
1827 const Type *IntPtrTy) {
1828 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001829 default:
1830 assert(!"Invalid CastOp");
1831 case Instruction::Trunc:
1832 case Instruction::ZExt:
1833 case Instruction::SExt:
1834 case Instruction::FPTrunc:
1835 case Instruction::FPExt:
1836 case Instruction::UIToFP:
1837 case Instruction::SIToFP:
1838 case Instruction::FPToUI:
1839 case Instruction::FPToSI:
1840 return false; // These always modify bits
1841 case Instruction::BitCast:
1842 return true; // BitCast never modifies bits.
1843 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001844 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001845 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001846 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001847 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001848 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001849 }
1850}
1851
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001852/// @brief Determine if a cast is a no-op.
1853bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1854 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1855}
1856
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001857/// This function determines if a pair of casts can be eliminated and what
1858/// opcode should be used in the elimination. This assumes that there are two
1859/// instructions like this:
1860/// * %F = firstOpcode SrcTy %x to MidTy
1861/// * %S = secondOpcode MidTy %F to DstTy
1862/// The function returns a resultOpcode so these two casts can be replaced with:
1863/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1864/// If no such cast is permited, the function returns 0.
1865unsigned CastInst::isEliminableCastPair(
1866 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1867 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1868{
1869 // Define the 144 possibilities for these two cast instructions. The values
1870 // in this matrix determine what to do in a given situation and select the
1871 // case in the switch below. The rows correspond to firstOp, the columns
1872 // correspond to secondOp. In looking at the table below, keep in mind
1873 // the following cast properties:
1874 //
1875 // Size Compare Source Destination
1876 // Operator Src ? Size Type Sign Type Sign
1877 // -------- ------------ ------------------- ---------------------
1878 // TRUNC > Integer Any Integral Any
1879 // ZEXT < Integral Unsigned Integer Any
1880 // SEXT < Integral Signed Integer Any
1881 // FPTOUI n/a FloatPt n/a Integral Unsigned
1882 // FPTOSI n/a FloatPt n/a Integral Signed
1883 // UITOFP n/a Integral Unsigned FloatPt n/a
1884 // SITOFP n/a Integral Signed FloatPt n/a
1885 // FPTRUNC > FloatPt n/a FloatPt n/a
1886 // FPEXT < FloatPt n/a FloatPt n/a
1887 // PTRTOINT n/a Pointer n/a Integral Unsigned
1888 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001889 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001890 //
1891 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001892 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1893 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001894 // of the produced value (we no longer know the top-part is all zeros).
1895 // Further this conversion is often much more expensive for typical hardware,
1896 // and causes issues when building libgcc. We disallow fptosi+sext for the
1897 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 const unsigned numCastOps =
1899 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1900 static const uint8_t CastResults[numCastOps][numCastOps] = {
1901 // T F F U S F F P I B -+
1902 // R Z S P P I I T P 2 N T |
1903 // U E E 2 2 2 2 R E I T C +- secondOp
1904 // N X X U S F F N X N 2 V |
1905 // C T T I I P P C T T P T -+
1906 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1907 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1908 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001909 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1910 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001911 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1912 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1913 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1914 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1915 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1916 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1917 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1918 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00001919
1920 // If either of the casts are a bitcast from scalar to vector, disallow the
1921 // merging.
1922 if ((firstOp == Instruction::BitCast &&
1923 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
1924 (secondOp == Instruction::BitCast &&
1925 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
1926 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001927
1928 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1929 [secondOp-Instruction::CastOpsBegin];
1930 switch (ElimCase) {
1931 case 0:
1932 // categorically disallowed
1933 return 0;
1934 case 1:
1935 // allowed, use first cast's opcode
1936 return firstOp;
1937 case 2:
1938 // allowed, use second cast's opcode
1939 return secondOp;
1940 case 3:
1941 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00001942 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00001943 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001944 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001945 return firstOp;
1946 return 0;
1947 case 4:
1948 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00001949 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001950 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001951 return firstOp;
1952 return 0;
1953 case 5:
1954 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00001955 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001956 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001957 return secondOp;
1958 return 0;
1959 case 6:
1960 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00001961 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001962 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001963 return secondOp;
1964 return 0;
1965 case 7: {
1966 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00001967 if (!IntPtrTy)
1968 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001969 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1970 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001971 if (MidSize >= PtrSize)
1972 return Instruction::BitCast;
1973 return 0;
1974 }
1975 case 8: {
1976 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1977 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1978 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001979 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1980 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001981 if (SrcSize == DstSize)
1982 return Instruction::BitCast;
1983 else if (SrcSize < DstSize)
1984 return firstOp;
1985 return secondOp;
1986 }
1987 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1988 return Instruction::ZExt;
1989 case 10:
1990 // fpext followed by ftrunc is allowed if the bit size returned to is
1991 // the same as the original, in which case its just a bitcast
1992 if (SrcTy == DstTy)
1993 return Instruction::BitCast;
1994 return 0; // If the types are not the same we can't eliminate it.
1995 case 11:
1996 // bitcast followed by ptrtoint is allowed as long as the bitcast
1997 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00001998 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001999 return secondOp;
2000 return 0;
2001 case 12:
2002 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002003 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004 return firstOp;
2005 return 0;
2006 case 13: {
2007 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002008 if (!IntPtrTy)
2009 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002010 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2011 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2012 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013 if (SrcSize <= PtrSize && SrcSize == DstSize)
2014 return Instruction::BitCast;
2015 return 0;
2016 }
2017 case 99:
2018 // cast combination can't happen (error in input). This is for all cases
2019 // where the MidTy is not the same for the two cast instructions.
2020 assert(!"Invalid Cast Combination");
2021 return 0;
2022 default:
2023 assert(!"Error in CastResults table!!!");
2024 return 0;
2025 }
2026 return 0;
2027}
2028
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002029CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002030 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002031 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002032 // Construct and return the appropriate CastInst subclass
2033 switch (op) {
2034 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2035 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2036 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2037 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2038 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2039 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2040 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2041 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2042 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2043 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2044 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2045 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2046 default:
2047 assert(!"Invalid opcode provided");
2048 }
2049 return 0;
2050}
2051
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002052CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002053 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002054 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055 // Construct and return the appropriate CastInst subclass
2056 switch (op) {
2057 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2058 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2059 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2060 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2061 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2062 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2063 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2064 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2065 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2066 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2067 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2068 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2069 default:
2070 assert(!"Invalid opcode provided");
2071 }
2072 return 0;
2073}
2074
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002075CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002076 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002077 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002078 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002079 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2080 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002081}
2082
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002083CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002084 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002085 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002086 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002087 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2088 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002089}
2090
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002091CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002092 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002093 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002094 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002095 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2096 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002097}
2098
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002099CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002100 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002101 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002102 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002103 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2104 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002105}
2106
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002107CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002108 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002109 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002110 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002111 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2112 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002113}
2114
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002115CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002116 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002117 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002118 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002119 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2120 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002121}
2122
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002123CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002124 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002125 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002126 assert(S->getType()->isPointerTy() && "Invalid cast");
2127 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002128 "Invalid cast");
2129
Duncan Sands9dff9be2010-02-15 16:12:20 +00002130 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002131 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2132 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002133}
2134
2135/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002136CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002137 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002138 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002139 assert(S->getType()->isPointerTy() && "Invalid cast");
2140 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002141 "Invalid cast");
2142
Duncan Sands9dff9be2010-02-15 16:12:20 +00002143 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002144 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2145 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002146}
2147
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002148CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002149 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002150 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002151 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002152 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002153 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2154 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002155 Instruction::CastOps opcode =
2156 (SrcBits == DstBits ? Instruction::BitCast :
2157 (SrcBits > DstBits ? Instruction::Trunc :
2158 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002159 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002160}
2161
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002162CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002163 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002164 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002165 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002166 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002167 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2168 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002169 Instruction::CastOps opcode =
2170 (SrcBits == DstBits ? Instruction::BitCast :
2171 (SrcBits > DstBits ? Instruction::Trunc :
2172 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002173 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002174}
2175
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002176CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002177 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002178 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002179 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002180 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002181 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2182 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002183 Instruction::CastOps opcode =
2184 (SrcBits == DstBits ? Instruction::BitCast :
2185 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002186 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002187}
2188
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002189CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002190 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002191 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002192 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002193 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002194 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2195 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002196 Instruction::CastOps opcode =
2197 (SrcBits == DstBits ? Instruction::BitCast :
2198 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002199 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002200}
2201
Duncan Sands55e50902008-01-06 10:12:28 +00002202// Check whether it is valid to call getCastOpcode for these types.
2203// This routine must be kept in sync with getCastOpcode.
2204bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2205 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2206 return false;
2207
2208 if (SrcTy == DestTy)
2209 return true;
2210
Duncan Sandsa8514532011-05-18 07:13:41 +00002211 if (const VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2212 if (const VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2213 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2214 // An element by element cast. Valid if casting the elements is valid.
2215 SrcTy = SrcVecTy->getElementType();
2216 DestTy = DestVecTy->getElementType();
2217 }
2218
Duncan Sands55e50902008-01-06 10:12:28 +00002219 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002220 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2221 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002222
2223 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002224 if (DestTy->isIntegerTy()) { // Casting to integral
2225 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002226 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002227 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002228 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002229 } else if (SrcTy->isVectorTy()) { // Casting from vector
2230 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002231 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002232 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002233 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002234 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2235 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002236 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002237 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002238 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002239 } else if (SrcTy->isVectorTy()) { // Casting from vector
2240 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002241 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002242 return false;
2243 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002244 } else if (DestTy->isVectorTy()) { // Casting to vector
2245 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002246 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002247 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002248 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002249 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002250 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002251 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002252 return false;
2253 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002254 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002255 if (SrcTy->isVectorTy()) {
2256 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002257 } else {
2258 return false;
2259 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002260 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002261 return false;
2262 }
2263}
2264
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002265// Provide a way to get a "cast" where the cast opcode is inferred from the
2266// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002267// logic in the castIsValid function below. This axiom should hold:
2268// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2269// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002271// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002272Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002273CastInst::getCastOpcode(
2274 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275 const Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002276
Duncan Sands55e50902008-01-06 10:12:28 +00002277 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2278 "Only first class types are castable!");
2279
Duncan Sandsa8514532011-05-18 07:13:41 +00002280 if (SrcTy == DestTy)
2281 return BitCast;
2282
2283 if (const VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2284 if (const VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2285 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2286 // An element by element cast. Find the appropriate opcode based on the
2287 // element types.
2288 SrcTy = SrcVecTy->getElementType();
2289 DestTy = DestVecTy->getElementType();
2290 }
2291
2292 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002293 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2294 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002295
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002297 if (DestTy->isIntegerTy()) { // Casting to integral
2298 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 if (DestBits < SrcBits)
2300 return Trunc; // int -> smaller int
2301 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002302 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002303 return SExt; // signed -> SEXT
2304 else
2305 return ZExt; // unsigned -> ZEXT
2306 } else {
2307 return BitCast; // Same size, No-op cast
2308 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002309 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002310 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 return FPToSI; // FP -> sint
2312 else
2313 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002314 } else if (SrcTy->isVectorTy()) {
2315 assert(DestBits == SrcBits &&
2316 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317 return BitCast; // Same size, no-op cast
2318 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002319 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320 "Casting from a value that is not first-class type");
2321 return PtrToInt; // ptr -> int
2322 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002323 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2324 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002325 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326 return SIToFP; // sint -> FP
2327 else
2328 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002329 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330 if (DestBits < SrcBits) {
2331 return FPTrunc; // FP -> smaller FP
2332 } else if (DestBits > SrcBits) {
2333 return FPExt; // FP -> larger FP
2334 } else {
2335 return BitCast; // same size, no-op cast
2336 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002337 } else if (SrcTy->isVectorTy()) {
2338 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002339 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002340 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002341 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002342 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002343 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002344 } else if (DestTy->isVectorTy()) {
2345 assert(DestBits == SrcBits &&
2346 "Illegal cast to vector (wrong type or size)");
2347 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002348 } else if (DestTy->isPointerTy()) {
2349 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002350 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002351 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352 return IntToPtr; // int -> ptr
2353 } else {
2354 assert(!"Casting pointer to other than pointer or int");
2355 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002356 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002357 if (SrcTy->isVectorTy()) {
2358 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002359 return BitCast; // 64-bit vector to MMX
2360 } else {
2361 assert(!"Illegal cast to X86_MMX");
2362 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 } else {
2364 assert(!"Casting to type that is not first-class");
2365 }
2366
2367 // If we fall through to here we probably hit an assertion cast above
2368 // and assertions are not turned on. Anything we return is an error, so
2369 // BitCast is as good a choice as any.
2370 return BitCast;
2371}
2372
2373//===----------------------------------------------------------------------===//
2374// CastInst SubClass Constructors
2375//===----------------------------------------------------------------------===//
2376
2377/// Check that the construction parameters for a CastInst are correct. This
2378/// could be broken out into the separate constructors but it is useful to have
2379/// it in one place and to eliminate the redundant code for getting the sizes
2380/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002381bool
2382CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002383
2384 // Check for type sanity on the arguments
2385 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002386 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2387 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388 return false;
2389
2390 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002391 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2392 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393
Duncan Sands7f646562011-05-18 09:21:57 +00002394 // If these are vector types, get the lengths of the vectors (using zero for
2395 // scalar types means that checking that vector lengths match also checks that
2396 // scalars are not being converted to vectors or vectors to scalars).
2397 unsigned SrcLength = SrcTy->isVectorTy() ?
2398 cast<VectorType>(SrcTy)->getNumElements() : 0;
2399 unsigned DstLength = DstTy->isVectorTy() ?
2400 cast<VectorType>(DstTy)->getNumElements() : 0;
2401
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 // Switch on the opcode provided
2403 switch (op) {
2404 default: return false; // This is an input error
2405 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002406 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2407 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002409 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2410 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002412 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2413 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002415 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2416 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002418 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2419 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002422 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2423 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002426 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2427 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002429 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002431 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432 case Instruction::BitCast:
2433 // BitCast implies a no-op cast of type only. No bits change.
2434 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002435 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436 return false;
2437
Duncan Sands55e50902008-01-06 10:12:28 +00002438 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002439 // these cases, the cast is okay if the source and destination bit widths
2440 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002441 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 }
2443}
2444
2445TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002446 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002448 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449}
2450
2451TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002452 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002454 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455}
2456
2457ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002458 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002460 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461}
2462
2463ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002464 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002466 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467}
2468SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002469 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002470) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002471 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472}
2473
Jeff Cohencc08c832006-12-02 02:22:01 +00002474SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002475 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002476) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002477 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478}
2479
2480FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002481 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002482) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002483 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484}
2485
2486FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002487 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002489 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490}
2491
2492FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002493 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002495 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496}
2497
2498FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002499 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002501 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502}
2503
2504UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002505 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002507 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508}
2509
2510UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002511 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002513 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514}
2515
2516SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002517 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002519 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520}
2521
2522SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002523 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002525 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526}
2527
2528FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002529 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002531 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532}
2533
2534FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002535 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002537 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538}
2539
2540FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002541 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002543 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544}
2545
2546FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002547 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002549 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550}
2551
2552PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002553 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002554) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002555 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556}
2557
2558PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002559 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002561 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562}
2563
2564IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002565 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002567 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568}
2569
2570IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002571 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002573 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574}
2575
2576BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002577 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002578) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002579 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580}
2581
2582BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002583 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002584) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002585 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002587
2588//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002589// CmpInst Classes
2590//===----------------------------------------------------------------------===//
2591
Chris Lattneraec33da2010-01-22 06:25:37 +00002592void CmpInst::Anchor() const {}
2593
Nate Begemand2195702008-05-12 19:01:56 +00002594CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002595 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002596 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002597 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002598 OperandTraits<CmpInst>::op_begin(this),
2599 OperandTraits<CmpInst>::operands(this),
2600 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002601 Op<0>() = LHS;
2602 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002603 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002604 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002605}
Gabor Greiff6caff662008-05-10 08:32:32 +00002606
Nate Begemand2195702008-05-12 19:01:56 +00002607CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002608 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002609 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002610 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002611 OperandTraits<CmpInst>::op_begin(this),
2612 OperandTraits<CmpInst>::operands(this),
2613 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002614 Op<0>() = LHS;
2615 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002616 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002617 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002618}
2619
2620CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002621CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002622 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002623 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002624 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002625 if (InsertBefore)
2626 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2627 S1, S2, Name);
2628 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002629 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002630 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002631 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002632
2633 if (InsertBefore)
2634 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2635 S1, S2, Name);
2636 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002637 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002638 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002639}
2640
2641CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002642CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002643 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002644 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002645 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2646 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002647 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002648 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2649 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002650}
2651
2652void CmpInst::swapOperands() {
2653 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2654 IC->swapOperands();
2655 else
2656 cast<FCmpInst>(this)->swapOperands();
2657}
2658
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002659bool CmpInst::isCommutative() const {
2660 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002661 return IC->isCommutative();
2662 return cast<FCmpInst>(this)->isCommutative();
2663}
2664
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002665bool CmpInst::isEquality() const {
2666 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002667 return IC->isEquality();
2668 return cast<FCmpInst>(this)->isEquality();
2669}
2670
2671
Dan Gohman4e724382008-05-31 02:47:54 +00002672CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002673 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002674 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002675 case ICMP_EQ: return ICMP_NE;
2676 case ICMP_NE: return ICMP_EQ;
2677 case ICMP_UGT: return ICMP_ULE;
2678 case ICMP_ULT: return ICMP_UGE;
2679 case ICMP_UGE: return ICMP_ULT;
2680 case ICMP_ULE: return ICMP_UGT;
2681 case ICMP_SGT: return ICMP_SLE;
2682 case ICMP_SLT: return ICMP_SGE;
2683 case ICMP_SGE: return ICMP_SLT;
2684 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002685
Dan Gohman4e724382008-05-31 02:47:54 +00002686 case FCMP_OEQ: return FCMP_UNE;
2687 case FCMP_ONE: return FCMP_UEQ;
2688 case FCMP_OGT: return FCMP_ULE;
2689 case FCMP_OLT: return FCMP_UGE;
2690 case FCMP_OGE: return FCMP_ULT;
2691 case FCMP_OLE: return FCMP_UGT;
2692 case FCMP_UEQ: return FCMP_ONE;
2693 case FCMP_UNE: return FCMP_OEQ;
2694 case FCMP_UGT: return FCMP_OLE;
2695 case FCMP_ULT: return FCMP_OGE;
2696 case FCMP_UGE: return FCMP_OLT;
2697 case FCMP_ULE: return FCMP_OGT;
2698 case FCMP_ORD: return FCMP_UNO;
2699 case FCMP_UNO: return FCMP_ORD;
2700 case FCMP_TRUE: return FCMP_FALSE;
2701 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002702 }
2703}
2704
Reid Spencer266e42b2006-12-23 06:05:41 +00002705ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2706 switch (pred) {
2707 default: assert(! "Unknown icmp predicate!");
2708 case ICMP_EQ: case ICMP_NE:
2709 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2710 return pred;
2711 case ICMP_UGT: return ICMP_SGT;
2712 case ICMP_ULT: return ICMP_SLT;
2713 case ICMP_UGE: return ICMP_SGE;
2714 case ICMP_ULE: return ICMP_SLE;
2715 }
2716}
2717
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002718ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2719 switch (pred) {
2720 default: assert(! "Unknown icmp predicate!");
2721 case ICMP_EQ: case ICMP_NE:
2722 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2723 return pred;
2724 case ICMP_SGT: return ICMP_UGT;
2725 case ICMP_SLT: return ICMP_ULT;
2726 case ICMP_SGE: return ICMP_UGE;
2727 case ICMP_SLE: return ICMP_ULE;
2728 }
2729}
2730
Reid Spencer0286bc12007-02-28 22:00:54 +00002731/// Initialize a set of values that all satisfy the condition with C.
2732///
2733ConstantRange
2734ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2735 APInt Lower(C);
2736 APInt Upper(C);
2737 uint32_t BitWidth = C.getBitWidth();
2738 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002739 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002740 case ICmpInst::ICMP_EQ: Upper++; break;
2741 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002742 case ICmpInst::ICMP_ULT:
2743 Lower = APInt::getMinValue(BitWidth);
2744 // Check for an empty-set condition.
2745 if (Lower == Upper)
2746 return ConstantRange(BitWidth, /*isFullSet=*/false);
2747 break;
2748 case ICmpInst::ICMP_SLT:
2749 Lower = APInt::getSignedMinValue(BitWidth);
2750 // Check for an empty-set condition.
2751 if (Lower == Upper)
2752 return ConstantRange(BitWidth, /*isFullSet=*/false);
2753 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002754 case ICmpInst::ICMP_UGT:
2755 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002756 // Check for an empty-set condition.
2757 if (Lower == Upper)
2758 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002759 break;
2760 case ICmpInst::ICMP_SGT:
2761 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002762 // Check for an empty-set condition.
2763 if (Lower == Upper)
2764 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002765 break;
2766 case ICmpInst::ICMP_ULE:
2767 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002768 // Check for a full-set condition.
2769 if (Lower == Upper)
2770 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002771 break;
2772 case ICmpInst::ICMP_SLE:
2773 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002774 // Check for a full-set condition.
2775 if (Lower == Upper)
2776 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002777 break;
2778 case ICmpInst::ICMP_UGE:
2779 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002780 // Check for a full-set condition.
2781 if (Lower == Upper)
2782 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002783 break;
2784 case ICmpInst::ICMP_SGE:
2785 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002786 // Check for a full-set condition.
2787 if (Lower == Upper)
2788 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002789 break;
2790 }
2791 return ConstantRange(Lower, Upper);
2792}
2793
Dan Gohman4e724382008-05-31 02:47:54 +00002794CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002795 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002796 default: assert(!"Unknown cmp predicate!");
2797 case ICMP_EQ: case ICMP_NE:
2798 return pred;
2799 case ICMP_SGT: return ICMP_SLT;
2800 case ICMP_SLT: return ICMP_SGT;
2801 case ICMP_SGE: return ICMP_SLE;
2802 case ICMP_SLE: return ICMP_SGE;
2803 case ICMP_UGT: return ICMP_ULT;
2804 case ICMP_ULT: return ICMP_UGT;
2805 case ICMP_UGE: return ICMP_ULE;
2806 case ICMP_ULE: return ICMP_UGE;
2807
Reid Spencerd9436b62006-11-20 01:22:35 +00002808 case FCMP_FALSE: case FCMP_TRUE:
2809 case FCMP_OEQ: case FCMP_ONE:
2810 case FCMP_UEQ: case FCMP_UNE:
2811 case FCMP_ORD: case FCMP_UNO:
2812 return pred;
2813 case FCMP_OGT: return FCMP_OLT;
2814 case FCMP_OLT: return FCMP_OGT;
2815 case FCMP_OGE: return FCMP_OLE;
2816 case FCMP_OLE: return FCMP_OGE;
2817 case FCMP_UGT: return FCMP_ULT;
2818 case FCMP_ULT: return FCMP_UGT;
2819 case FCMP_UGE: return FCMP_ULE;
2820 case FCMP_ULE: return FCMP_UGE;
2821 }
2822}
2823
Reid Spencer266e42b2006-12-23 06:05:41 +00002824bool CmpInst::isUnsigned(unsigned short predicate) {
2825 switch (predicate) {
2826 default: return false;
2827 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2828 case ICmpInst::ICMP_UGE: return true;
2829 }
2830}
2831
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002832bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002833 switch (predicate) {
2834 default: return false;
2835 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2836 case ICmpInst::ICMP_SGE: return true;
2837 }
2838}
2839
2840bool CmpInst::isOrdered(unsigned short predicate) {
2841 switch (predicate) {
2842 default: return false;
2843 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2844 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2845 case FCmpInst::FCMP_ORD: return true;
2846 }
2847}
2848
2849bool CmpInst::isUnordered(unsigned short predicate) {
2850 switch (predicate) {
2851 default: return false;
2852 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2853 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2854 case FCmpInst::FCMP_UNO: return true;
2855 }
2856}
2857
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002858bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2859 switch(predicate) {
2860 default: return false;
2861 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2862 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2863 }
2864}
2865
2866bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2867 switch(predicate) {
2868 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2869 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2870 default: return false;
2871 }
2872}
2873
2874
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002875//===----------------------------------------------------------------------===//
2876// SwitchInst Implementation
2877//===----------------------------------------------------------------------===//
2878
Chris Lattnerbaf00152010-11-17 05:41:46 +00002879void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
2880 assert(Value && Default && NumReserved);
2881 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002882 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002883 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002884
Gabor Greif2d3024d2008-05-26 21:33:52 +00002885 OperandList[0] = Value;
2886 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002887}
2888
Chris Lattner2195fc42007-02-24 00:55:48 +00002889/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2890/// switch on and a default destination. The number of additional cases can
2891/// be specified here to make memory allocation more efficient. This
2892/// constructor can also autoinsert before another instruction.
2893SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2894 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002895 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2896 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002897 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002898}
2899
2900/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2901/// switch on and a default destination. The number of additional cases can
2902/// be specified here to make memory allocation more efficient. This
2903/// constructor also autoinserts at the end of the specified BasicBlock.
2904SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2905 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002906 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2907 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002908 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002909}
2910
Misha Brukmanb1c93172005-04-21 23:48:37 +00002911SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00002912 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
2913 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
2914 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002915 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00002916 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002917 OL[i] = InOL[i];
2918 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002919 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002920 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002921}
2922
Gordon Henriksen14a55692007-12-10 02:14:30 +00002923SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00002924 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002925}
2926
2927
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002928/// addCase - Add an entry to the switch instruction...
2929///
Chris Lattner47ac1872005-02-24 05:32:09 +00002930void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002931 unsigned OpNo = NumOperands;
2932 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00002933 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002934 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002935 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002936 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002937 OperandList[OpNo] = OnVal;
2938 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002939}
2940
2941/// removeCase - This method removes the specified successor from the switch
2942/// instruction. Note that this cannot be used to remove the default
2943/// destination (successor #0).
2944///
2945void SwitchInst::removeCase(unsigned idx) {
2946 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002947 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2948
2949 unsigned NumOps = getNumOperands();
2950 Use *OL = OperandList;
2951
Jay Foad14277722011-02-01 09:22:34 +00002952 // Overwrite this case with the end of the list.
2953 if ((idx + 1) * 2 != NumOps) {
2954 OL[idx * 2] = OL[NumOps - 2];
2955 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002956 }
2957
2958 // Nuke the last value.
2959 OL[NumOps-2].set(0);
2960 OL[NumOps-2+1].set(0);
2961 NumOperands = NumOps-2;
2962}
2963
Jay Foade98f29d2011-04-01 08:00:58 +00002964/// growOperands - grow operands - This grows the operand list in response
2965/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002966///
Jay Foade98f29d2011-04-01 08:00:58 +00002967void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002968 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00002969 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002970
2971 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002972 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002973 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002974 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002975 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002976 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002977 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00002978 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002979}
2980
2981
2982BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2983 return getSuccessor(idx);
2984}
2985unsigned SwitchInst::getNumSuccessorsV() const {
2986 return getNumSuccessors();
2987}
2988void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2989 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002990}
Chris Lattnerf22be932004-10-15 23:52:53 +00002991
Chris Lattner3ed871f2009-10-27 19:13:16 +00002992//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00002993// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00002994//===----------------------------------------------------------------------===//
2995
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002996void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002997 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00002998 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00002999 ReservedSpace = 1+NumDests;
3000 NumOperands = 1;
3001 OperandList = allocHungoffUses(ReservedSpace);
3002
3003 OperandList[0] = Address;
3004}
3005
3006
Jay Foade98f29d2011-04-01 08:00:58 +00003007/// growOperands - grow operands - This grows the operand list in response
3008/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003009///
Jay Foade98f29d2011-04-01 08:00:58 +00003010void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003011 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003012 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003013
3014 ReservedSpace = NumOps;
3015 Use *NewOps = allocHungoffUses(NumOps);
3016 Use *OldOps = OperandList;
3017 for (unsigned i = 0; i != e; ++i)
3018 NewOps[i] = OldOps[i];
3019 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003020 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003021}
3022
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003023IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3024 Instruction *InsertBefore)
3025: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003026 0, 0, InsertBefore) {
3027 init(Address, NumCases);
3028}
3029
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003030IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3031 BasicBlock *InsertAtEnd)
3032: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003033 0, 0, InsertAtEnd) {
3034 init(Address, NumCases);
3035}
3036
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003037IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3038 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003039 allocHungoffUses(IBI.getNumOperands()),
3040 IBI.getNumOperands()) {
3041 Use *OL = OperandList, *InOL = IBI.OperandList;
3042 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3043 OL[i] = InOL[i];
3044 SubclassOptionalData = IBI.SubclassOptionalData;
3045}
3046
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003047IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003048 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003049}
3050
3051/// addDestination - Add a destination.
3052///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003053void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003054 unsigned OpNo = NumOperands;
3055 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003056 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003057 // Initialize some new operands.
3058 assert(OpNo < ReservedSpace && "Growing didn't work!");
3059 NumOperands = OpNo+1;
3060 OperandList[OpNo] = DestBB;
3061}
3062
3063/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003064/// indirectbr instruction.
3065void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003066 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3067
3068 unsigned NumOps = getNumOperands();
3069 Use *OL = OperandList;
3070
3071 // Replace this value with the last one.
3072 OL[idx+1] = OL[NumOps-1];
3073
3074 // Nuke the last value.
3075 OL[NumOps-1].set(0);
3076 NumOperands = NumOps-1;
3077}
3078
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003079BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003080 return getSuccessor(idx);
3081}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003082unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003083 return getNumSuccessors();
3084}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003085void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003086 setSuccessor(idx, B);
3087}
3088
3089//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003090// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003091//===----------------------------------------------------------------------===//
3092
Chris Lattnerf22be932004-10-15 23:52:53 +00003093// Define these methods here so vtables don't get emitted into every translation
3094// unit that uses these classes.
3095
Devang Patel11cf3f42009-10-27 22:16:29 +00003096GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3097 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003098}
3099
Devang Patel11cf3f42009-10-27 22:16:29 +00003100BinaryOperator *BinaryOperator::clone_impl() const {
3101 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003102}
3103
Devang Patel11cf3f42009-10-27 22:16:29 +00003104FCmpInst* FCmpInst::clone_impl() const {
3105 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003106}
3107
Devang Patel11cf3f42009-10-27 22:16:29 +00003108ICmpInst* ICmpInst::clone_impl() const {
3109 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003110}
3111
Devang Patel11cf3f42009-10-27 22:16:29 +00003112ExtractValueInst *ExtractValueInst::clone_impl() const {
3113 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003114}
3115
Devang Patel11cf3f42009-10-27 22:16:29 +00003116InsertValueInst *InsertValueInst::clone_impl() const {
3117 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003118}
3119
Devang Patel11cf3f42009-10-27 22:16:29 +00003120AllocaInst *AllocaInst::clone_impl() const {
3121 return new AllocaInst(getAllocatedType(),
3122 (Value*)getOperand(0),
3123 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003124}
3125
Devang Patel11cf3f42009-10-27 22:16:29 +00003126LoadInst *LoadInst::clone_impl() const {
3127 return new LoadInst(getOperand(0),
3128 Twine(), isVolatile(),
3129 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003130}
3131
Devang Patel11cf3f42009-10-27 22:16:29 +00003132StoreInst *StoreInst::clone_impl() const {
3133 return new StoreInst(getOperand(0), getOperand(1),
3134 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003135}
3136
Devang Patel11cf3f42009-10-27 22:16:29 +00003137TruncInst *TruncInst::clone_impl() const {
3138 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003139}
3140
Devang Patel11cf3f42009-10-27 22:16:29 +00003141ZExtInst *ZExtInst::clone_impl() const {
3142 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003143}
3144
Devang Patel11cf3f42009-10-27 22:16:29 +00003145SExtInst *SExtInst::clone_impl() const {
3146 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003147}
3148
Devang Patel11cf3f42009-10-27 22:16:29 +00003149FPTruncInst *FPTruncInst::clone_impl() const {
3150 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003151}
3152
Devang Patel11cf3f42009-10-27 22:16:29 +00003153FPExtInst *FPExtInst::clone_impl() const {
3154 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003155}
3156
Devang Patel11cf3f42009-10-27 22:16:29 +00003157UIToFPInst *UIToFPInst::clone_impl() const {
3158 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003159}
3160
Devang Patel11cf3f42009-10-27 22:16:29 +00003161SIToFPInst *SIToFPInst::clone_impl() const {
3162 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003163}
3164
Devang Patel11cf3f42009-10-27 22:16:29 +00003165FPToUIInst *FPToUIInst::clone_impl() const {
3166 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003167}
3168
Devang Patel11cf3f42009-10-27 22:16:29 +00003169FPToSIInst *FPToSIInst::clone_impl() const {
3170 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003171}
3172
Devang Patel11cf3f42009-10-27 22:16:29 +00003173PtrToIntInst *PtrToIntInst::clone_impl() const {
3174 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003175}
3176
Devang Patel11cf3f42009-10-27 22:16:29 +00003177IntToPtrInst *IntToPtrInst::clone_impl() const {
3178 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003179}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003180
Devang Patel11cf3f42009-10-27 22:16:29 +00003181BitCastInst *BitCastInst::clone_impl() const {
3182 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003183}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003184
Devang Patel11cf3f42009-10-27 22:16:29 +00003185CallInst *CallInst::clone_impl() const {
3186 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003187}
3188
Devang Patel11cf3f42009-10-27 22:16:29 +00003189SelectInst *SelectInst::clone_impl() const {
3190 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003191}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003192
Devang Patel11cf3f42009-10-27 22:16:29 +00003193VAArgInst *VAArgInst::clone_impl() const {
3194 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003195}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003196
Devang Patel11cf3f42009-10-27 22:16:29 +00003197ExtractElementInst *ExtractElementInst::clone_impl() const {
3198 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003199}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003200
Devang Patel11cf3f42009-10-27 22:16:29 +00003201InsertElementInst *InsertElementInst::clone_impl() const {
3202 return InsertElementInst::Create(getOperand(0),
3203 getOperand(1),
3204 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003205}
3206
Devang Patel11cf3f42009-10-27 22:16:29 +00003207ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3208 return new ShuffleVectorInst(getOperand(0),
3209 getOperand(1),
3210 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003211}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003212
Devang Patel11cf3f42009-10-27 22:16:29 +00003213PHINode *PHINode::clone_impl() const {
3214 return new PHINode(*this);
3215}
3216
3217ReturnInst *ReturnInst::clone_impl() const {
3218 return new(getNumOperands()) ReturnInst(*this);
3219}
3220
3221BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003222 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003223}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003224
Devang Patel11cf3f42009-10-27 22:16:29 +00003225SwitchInst *SwitchInst::clone_impl() const {
3226 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003227}
3228
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003229IndirectBrInst *IndirectBrInst::clone_impl() const {
3230 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003231}
3232
3233
Devang Patel11cf3f42009-10-27 22:16:29 +00003234InvokeInst *InvokeInst::clone_impl() const {
3235 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003236}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003237
Devang Patel11cf3f42009-10-27 22:16:29 +00003238UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003239 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003240 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003241}
3242
Devang Patel11cf3f42009-10-27 22:16:29 +00003243UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003244 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003245 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003246}