blob: 75458babfbbbfbafdd4f36978505de2d39ded2ca [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
Chris Lattner229907c2011-07-18 04:54:35 +000065 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000066 // 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";
Chris Lattner229907c2011-07-18 04:54:35 +000069 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Chris Lattner88107952008-12-29 00:12:50 +000070 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
Jay Foad5bd375a2011-07-15 08:37:34 +0000177void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
178 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000179 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000180
Jay Foad5bd375a2011-07-15 08:37:34 +0000181#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000182 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000183 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
184
Jay Foad5bd375a2011-07-15 08:37:34 +0000185 assert((Args.size() == FTy->getNumParams() ||
186 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000187 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000188
189 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000190 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000191 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000192 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000193#endif
194
195 std::copy(Args.begin(), Args.end(), op_begin());
196 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000197}
198
Jay Foad5bd375a2011-07-15 08:37:34 +0000199void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000200 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000201 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000202
Jay Foad5bd375a2011-07-15 08:37:34 +0000203#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000204 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000205 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
206
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000207 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000208#endif
209
210 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000211}
212
Daniel Dunbar4975db62009-07-25 04:41:11 +0000213CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000214 Instruction *InsertBefore)
215 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
216 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000217 Instruction::Call,
218 OperandTraits<CallInst>::op_end(this) - 1,
219 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000220 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000221}
222
Daniel Dunbar4975db62009-07-25 04:41:11 +0000223CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000224 BasicBlock *InsertAtEnd)
225 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
226 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000227 Instruction::Call,
228 OperandTraits<CallInst>::op_end(this) - 1,
229 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000230 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000231}
232
Misha Brukmanb1c93172005-04-21 23:48:37 +0000233CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000234 : Instruction(CI.getType(), Instruction::Call,
235 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000236 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000237 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000238 setTailCall(CI.isTailCall());
239 setCallingConv(CI.getCallingConv());
240
Jay Foad5bd375a2011-07-15 08:37:34 +0000241 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000242 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000243}
244
Devang Patel4c758ea2008-09-25 21:00:45 +0000245void CallInst::addAttribute(unsigned i, Attributes attr) {
246 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000247 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000248 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000249}
250
Devang Patel4c758ea2008-09-25 21:00:45 +0000251void CallInst::removeAttribute(unsigned i, Attributes attr) {
252 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000253 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000254 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000255}
256
Devang Patelba3fa6c2008-09-23 23:03:40 +0000257bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000258 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000259 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000260 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000261 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000262 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000263}
264
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000265/// IsConstantOne - Return true only if val is constant int 1
266static bool IsConstantOne(Value *val) {
267 assert(val && "IsConstantOne does not work with NULL val");
268 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
269}
270
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000271static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000272 BasicBlock *InsertAtEnd, Type *IntPtrTy,
273 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000274 Value *ArraySize, Function *MallocF,
275 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000276 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000277 "createMalloc needs either InsertBefore or InsertAtEnd");
278
279 // malloc(type) becomes:
280 // bitcast (i8* malloc(typeSize)) to type*
281 // malloc(type, arraySize) becomes:
282 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000283 if (!ArraySize)
284 ArraySize = ConstantInt::get(IntPtrTy, 1);
285 else if (ArraySize->getType() != IntPtrTy) {
286 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000287 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
288 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000289 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000290 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
291 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000292 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000293
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000294 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000295 if (IsConstantOne(AllocSize)) {
296 AllocSize = ArraySize; // Operand * 1 = Operand
297 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
298 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
299 false /*ZExt*/);
300 // Malloc arg is constant product of type size and array size
301 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
302 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000303 // Multiply type size by the array size...
304 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000305 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
306 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000307 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000308 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
309 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000310 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000311 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000312
Victor Hernandez788eaab2009-09-18 19:20:02 +0000313 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000314 // Create the call to Malloc.
315 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
316 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000317 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000318 Value *MallocFunc = MallocF;
319 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000320 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000321 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000322 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000323 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000324 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000325 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000326 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000327 Result = MCall;
328 if (Result->getType() != AllocPtrType)
329 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000330 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000331 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000332 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000333 Result = MCall;
334 if (Result->getType() != AllocPtrType) {
335 InsertAtEnd->getInstList().push_back(MCall);
336 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000337 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000338 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000339 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000340 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000341 if (Function *F = dyn_cast<Function>(MallocFunc)) {
342 MCall->setCallingConv(F->getCallingConv());
343 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
344 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000345 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000346
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000347 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000348}
349
350/// CreateMalloc - Generate the IR for a call to malloc:
351/// 1. Compute the malloc call's argument as the specified type's size,
352/// possibly multiplied by the array size if the array size is not
353/// constant 1.
354/// 2. Call malloc with that argument.
355/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000356Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000357 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000358 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000359 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000360 const Twine &Name) {
361 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000362 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000363}
364
365/// CreateMalloc - Generate the IR for a call to malloc:
366/// 1. Compute the malloc call's argument as the specified type's size,
367/// possibly multiplied by the array size if the array size is not
368/// constant 1.
369/// 2. Call malloc with that argument.
370/// 3. Bitcast the result of the malloc call to the specified type.
371/// Note: This function does not add the bitcast to the basic block, that is the
372/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000373Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000374 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000375 Value *AllocSize, Value *ArraySize,
376 Function *MallocF, const Twine &Name) {
377 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000378 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000379}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000380
Victor Hernandeze2971492009-10-24 04:23:03 +0000381static Instruction* createFree(Value* Source, Instruction *InsertBefore,
382 BasicBlock *InsertAtEnd) {
383 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
384 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000385 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000386 "Can not free something of nonpointer type!");
387
388 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
389 Module* M = BB->getParent()->getParent();
390
Chris Lattner229907c2011-07-18 04:54:35 +0000391 Type *VoidTy = Type::getVoidTy(M->getContext());
392 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000393 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000394 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000395 CallInst* Result = NULL;
396 Value *PtrCast = Source;
397 if (InsertBefore) {
398 if (Source->getType() != IntPtrTy)
399 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
400 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
401 } else {
402 if (Source->getType() != IntPtrTy)
403 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
404 Result = CallInst::Create(FreeFunc, PtrCast, "");
405 }
406 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000407 if (Function *F = dyn_cast<Function>(FreeFunc))
408 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000409
410 return Result;
411}
412
413/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000414Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
415 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000416}
417
418/// CreateFree - Generate the IR for a call to the builtin free function.
419/// Note: This function does not add the call to the basic block, that is the
420/// responsibility of the caller.
421Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
422 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
423 assert(FreeCall && "CreateFree did not create a CallInst");
424 return FreeCall;
425}
426
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000427//===----------------------------------------------------------------------===//
428// InvokeInst Implementation
429//===----------------------------------------------------------------------===//
430
431void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000432 ArrayRef<Value *> Args, const Twine &NameStr) {
433 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000434 Op<-3>() = Fn;
435 Op<-2>() = IfNormal;
436 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000437
438#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000439 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000440 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000441
Jay Foad5bd375a2011-07-15 08:37:34 +0000442 assert(((Args.size() == FTy->getNumParams()) ||
443 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000444 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000445
Jay Foad5bd375a2011-07-15 08:37:34 +0000446 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000447 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000448 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000449 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000450#endif
451
452 std::copy(Args.begin(), Args.end(), op_begin());
453 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000454}
455
Misha Brukmanb1c93172005-04-21 23:48:37 +0000456InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000457 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000458 OperandTraits<InvokeInst>::op_end(this)
459 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000460 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000461 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000462 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000463 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000464 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000465}
466
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000467BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
468 return getSuccessor(idx);
469}
470unsigned InvokeInst::getNumSuccessorsV() const {
471 return getNumSuccessors();
472}
473void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
474 return setSuccessor(idx, B);
475}
476
Devang Patelba3fa6c2008-09-23 23:03:40 +0000477bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000478 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000479 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000480 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000481 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000482 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000483}
484
Devang Patel4c758ea2008-09-25 21:00:45 +0000485void InvokeInst::addAttribute(unsigned i, Attributes attr) {
486 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000487 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000488 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000489}
490
Devang Patel4c758ea2008-09-25 21:00:45 +0000491void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
492 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000493 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000494 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000495}
496
Duncan Sands5208d1a2007-11-28 17:07:01 +0000497
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000498//===----------------------------------------------------------------------===//
499// ReturnInst Implementation
500//===----------------------------------------------------------------------===//
501
Chris Lattner2195fc42007-02-24 00:55:48 +0000502ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000503 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000504 OperandTraits<ReturnInst>::op_end(this) -
505 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000506 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000507 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000508 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000509 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000510}
511
Owen Anderson55f1c092009-08-13 21:58:54 +0000512ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
513 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000514 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
515 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000516 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000517 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000518}
Owen Anderson55f1c092009-08-13 21:58:54 +0000519ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
520 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000521 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
522 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000523 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000524 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000525}
Owen Anderson55f1c092009-08-13 21:58:54 +0000526ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
527 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000528 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000529}
530
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000531unsigned ReturnInst::getNumSuccessorsV() const {
532 return getNumSuccessors();
533}
534
Devang Patelae682fb2008-02-26 17:56:20 +0000535/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
536/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000537void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000538 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000539}
540
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000541BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000542 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543 return 0;
544}
545
Devang Patel59643e52008-02-23 00:35:18 +0000546ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000547}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000548
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000549//===----------------------------------------------------------------------===//
550// UnwindInst Implementation
551//===----------------------------------------------------------------------===//
552
Owen Anderson55f1c092009-08-13 21:58:54 +0000553UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
554 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
555 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000556}
Owen Anderson55f1c092009-08-13 21:58:54 +0000557UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
558 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
559 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000560}
561
562
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000563unsigned UnwindInst::getNumSuccessorsV() const {
564 return getNumSuccessors();
565}
566
567void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000568 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000569}
570
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000572 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000573 return 0;
574}
575
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000576//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000577// UnreachableInst Implementation
578//===----------------------------------------------------------------------===//
579
Owen Anderson55f1c092009-08-13 21:58:54 +0000580UnreachableInst::UnreachableInst(LLVMContext &Context,
581 Instruction *InsertBefore)
582 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
583 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000584}
Owen Anderson55f1c092009-08-13 21:58:54 +0000585UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
586 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
587 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000588}
589
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590unsigned UnreachableInst::getNumSuccessorsV() const {
591 return getNumSuccessors();
592}
593
594void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendlingad088e62011-07-30 05:42:50 +0000595 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596}
597
598BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendlingad088e62011-07-30 05:42:50 +0000599 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000600 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000601}
602
603//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000604// BranchInst Implementation
605//===----------------------------------------------------------------------===//
606
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000607void BranchInst::AssertOK() {
608 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000609 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000610 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000611}
612
Chris Lattner2195fc42007-02-24 00:55:48 +0000613BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000614 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000615 OperandTraits<BranchInst>::op_end(this) - 1,
616 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000617 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000618 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000619}
620BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
621 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000622 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000623 OperandTraits<BranchInst>::op_end(this) - 3,
624 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000625 Op<-1>() = IfTrue;
626 Op<-2>() = IfFalse;
627 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000628#ifndef NDEBUG
629 AssertOK();
630#endif
631}
632
633BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000634 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000635 OperandTraits<BranchInst>::op_end(this) - 1,
636 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000637 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000638 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000639}
640
641BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
642 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000643 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000644 OperandTraits<BranchInst>::op_end(this) - 3,
645 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000646 Op<-1>() = IfTrue;
647 Op<-2>() = IfFalse;
648 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000649#ifndef NDEBUG
650 AssertOK();
651#endif
652}
653
654
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000656 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000657 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
658 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000659 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660 if (BI.getNumOperands() != 1) {
661 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000662 Op<-3>() = BI.Op<-3>();
663 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000664 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000665 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000666}
667
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000668BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
669 return getSuccessor(idx);
670}
671unsigned BranchInst::getNumSuccessorsV() const {
672 return getNumSuccessors();
673}
674void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
675 setSuccessor(idx, B);
676}
677
678
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000679//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000680// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000681//===----------------------------------------------------------------------===//
682
Owen Andersonb6b25302009-07-14 23:09:55 +0000683static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000684 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000685 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000686 else {
687 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000688 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000689 assert(Amt->getType()->isIntegerTy() &&
690 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000691 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000692 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000693}
694
Chris Lattner229907c2011-07-18 04:54:35 +0000695AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000696 const Twine &Name, Instruction *InsertBefore)
697 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
698 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
699 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000700 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000701 setName(Name);
702}
703
Chris Lattner229907c2011-07-18 04:54:35 +0000704AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000705 const Twine &Name, BasicBlock *InsertAtEnd)
706 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
707 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
708 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000709 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000710 setName(Name);
711}
712
Chris Lattner229907c2011-07-18 04:54:35 +0000713AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000714 Instruction *InsertBefore)
715 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
716 getAISize(Ty->getContext(), 0), InsertBefore) {
717 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000718 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000719 setName(Name);
720}
721
Chris Lattner229907c2011-07-18 04:54:35 +0000722AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000723 BasicBlock *InsertAtEnd)
724 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
725 getAISize(Ty->getContext(), 0), InsertAtEnd) {
726 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000727 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000728 setName(Name);
729}
730
Chris Lattner229907c2011-07-18 04:54:35 +0000731AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000732 const Twine &Name, Instruction *InsertBefore)
733 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000734 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000735 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000736 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000737 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000738}
739
Chris Lattner229907c2011-07-18 04:54:35 +0000740AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000741 const Twine &Name, BasicBlock *InsertAtEnd)
742 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000743 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000744 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000745 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000746 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747}
748
Gordon Henriksen14a55692007-12-10 02:14:30 +0000749// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000750AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000751}
752
Victor Hernandez8acf2952009-10-23 21:09:37 +0000753void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000754 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000755 assert(Align <= MaximumAlignment &&
756 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000757 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000758 assert(getAlignment() == Align && "Alignment representation error!");
759}
760
Victor Hernandez8acf2952009-10-23 21:09:37 +0000761bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000762 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000763 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000764 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765}
766
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000767Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000768 return getType()->getElementType();
769}
770
Chris Lattner8b291e62008-11-26 02:54:17 +0000771/// isStaticAlloca - Return true if this alloca is in the entry block of the
772/// function and is a constant size. If so, the code generator will fold it
773/// into the prolog/epilog code, so it is basically free.
774bool AllocaInst::isStaticAlloca() const {
775 // Must be constant size.
776 if (!isa<ConstantInt>(getArraySize())) return false;
777
778 // Must be in the entry block.
779 const BasicBlock *Parent = getParent();
780 return Parent == &Parent->getParent()->front();
781}
782
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000783//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784// LoadInst Implementation
785//===----------------------------------------------------------------------===//
786
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000787void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000788 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000789 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790}
791
Daniel Dunbar4975db62009-07-25 04:41:11 +0000792LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000793 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000794 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000795 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000796 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000797 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000798 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000799}
800
Daniel Dunbar4975db62009-07-25 04:41:11 +0000801LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000802 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000803 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000804 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000805 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000806 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000807 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000808}
809
Daniel Dunbar4975db62009-07-25 04:41:11 +0000810LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000812 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000813 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000814 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000815 setAlignment(0);
816 AssertOK();
817 setName(Name);
818}
819
Daniel Dunbar4975db62009-07-25 04:41:11 +0000820LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000821 unsigned Align, Instruction *InsertBef)
822 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
823 Load, Ptr, InsertBef) {
824 setVolatile(isVolatile);
825 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000826 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000827 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828}
829
Daniel Dunbar4975db62009-07-25 04:41:11 +0000830LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000831 unsigned Align, BasicBlock *InsertAE)
832 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
833 Load, Ptr, InsertAE) {
834 setVolatile(isVolatile);
835 setAlignment(Align);
836 AssertOK();
837 setName(Name);
838}
839
Daniel Dunbar4975db62009-07-25 04:41:11 +0000840LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000841 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000842 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000843 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000844 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000845 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000846 AssertOK();
847 setName(Name);
848}
849
Daniel Dunbar27096822009-08-11 18:11:15 +0000850
851
852LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
853 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
854 Load, Ptr, InsertBef) {
855 setVolatile(false);
856 setAlignment(0);
857 AssertOK();
858 if (Name && Name[0]) setName(Name);
859}
860
861LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
862 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
863 Load, Ptr, InsertAE) {
864 setVolatile(false);
865 setAlignment(0);
866 AssertOK();
867 if (Name && Name[0]) setName(Name);
868}
869
870LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
871 Instruction *InsertBef)
872: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
873 Load, Ptr, InsertBef) {
874 setVolatile(isVolatile);
875 setAlignment(0);
876 AssertOK();
877 if (Name && Name[0]) setName(Name);
878}
879
880LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
881 BasicBlock *InsertAE)
882 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
883 Load, Ptr, InsertAE) {
884 setVolatile(isVolatile);
885 setAlignment(0);
886 AssertOK();
887 if (Name && Name[0]) setName(Name);
888}
889
Christopher Lamb84485702007-04-22 19:24:39 +0000890void LoadInst::setAlignment(unsigned Align) {
891 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000892 assert(Align <= MaximumAlignment &&
893 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000894 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
895 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000896 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000897}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898
899//===----------------------------------------------------------------------===//
900// StoreInst Implementation
901//===----------------------------------------------------------------------===//
902
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000904 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000905 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000906 "Ptr must have pointer type!");
907 assert(getOperand(0)->getType() ==
908 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000909 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000910}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000911
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000912
913StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000914 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000915 OperandTraits<StoreInst>::op_begin(this),
916 OperandTraits<StoreInst>::operands(this),
917 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000918 Op<0>() = val;
919 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000920 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000921 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922 AssertOK();
923}
924
925StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000926 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000927 OperandTraits<StoreInst>::op_begin(this),
928 OperandTraits<StoreInst>::operands(this),
929 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000930 Op<0>() = val;
931 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000932 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000933 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000934 AssertOK();
935}
936
Misha Brukmanb1c93172005-04-21 23:48:37 +0000937StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000938 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000939 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000940 OperandTraits<StoreInst>::op_begin(this),
941 OperandTraits<StoreInst>::operands(this),
942 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000943 Op<0>() = val;
944 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000945 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000946 setAlignment(0);
947 AssertOK();
948}
949
950StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
951 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000952 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000953 OperandTraits<StoreInst>::op_begin(this),
954 OperandTraits<StoreInst>::operands(this),
955 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000956 Op<0>() = val;
957 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000958 setVolatile(isVolatile);
959 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000960 AssertOK();
961}
962
Misha Brukmanb1c93172005-04-21 23:48:37 +0000963StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000964 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000965 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000966 OperandTraits<StoreInst>::op_begin(this),
967 OperandTraits<StoreInst>::operands(this),
968 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000969 Op<0>() = val;
970 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000971 setVolatile(isVolatile);
972 setAlignment(Align);
973 AssertOK();
974}
975
976StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000977 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000978 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000979 OperandTraits<StoreInst>::op_begin(this),
980 OperandTraits<StoreInst>::operands(this),
981 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000982 Op<0>() = val;
983 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000984 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000985 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000986 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987}
988
Christopher Lamb84485702007-04-22 19:24:39 +0000989void StoreInst::setAlignment(unsigned Align) {
990 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000991 assert(Align <= MaximumAlignment &&
992 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000993 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
994 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000995 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000996}
997
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000998//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000999// AtomicCmpXchgInst Implementation
1000//===----------------------------------------------------------------------===//
1001
1002void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1003 AtomicOrdering Ordering,
1004 SynchronizationScope SynchScope) {
1005 Op<0>() = Ptr;
1006 Op<1>() = Cmp;
1007 Op<2>() = NewVal;
1008 setOrdering(Ordering);
1009 setSynchScope(SynchScope);
1010
1011 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1012 "All operands must be non-null!");
1013 assert(getOperand(0)->getType()->isPointerTy() &&
1014 "Ptr must have pointer type!");
1015 assert(getOperand(1)->getType() ==
1016 cast<PointerType>(getOperand(0)->getType())->getElementType()
1017 && "Ptr must be a pointer to Cmp type!");
1018 assert(getOperand(2)->getType() ==
1019 cast<PointerType>(getOperand(0)->getType())->getElementType()
1020 && "Ptr must be a pointer to NewVal type!");
1021 assert(Ordering != NotAtomic &&
1022 "AtomicCmpXchg instructions must be atomic!");
1023}
1024
1025AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1026 AtomicOrdering Ordering,
1027 SynchronizationScope SynchScope,
1028 Instruction *InsertBefore)
1029 : Instruction(Cmp->getType(), AtomicCmpXchg,
1030 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1031 OperandTraits<AtomicCmpXchgInst>::operands(this),
1032 InsertBefore) {
1033 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1034}
1035
1036AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1037 AtomicOrdering Ordering,
1038 SynchronizationScope SynchScope,
1039 BasicBlock *InsertAtEnd)
1040 : Instruction(Cmp->getType(), AtomicCmpXchg,
1041 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1042 OperandTraits<AtomicCmpXchgInst>::operands(this),
1043 InsertAtEnd) {
1044 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1045}
1046
1047//===----------------------------------------------------------------------===//
1048// AtomicRMWInst Implementation
1049//===----------------------------------------------------------------------===//
1050
1051void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1052 AtomicOrdering Ordering,
1053 SynchronizationScope SynchScope) {
1054 Op<0>() = Ptr;
1055 Op<1>() = Val;
1056 setOperation(Operation);
1057 setOrdering(Ordering);
1058 setSynchScope(SynchScope);
1059
1060 assert(getOperand(0) && getOperand(1) &&
1061 "All operands must be non-null!");
1062 assert(getOperand(0)->getType()->isPointerTy() &&
1063 "Ptr must have pointer type!");
1064 assert(getOperand(1)->getType() ==
1065 cast<PointerType>(getOperand(0)->getType())->getElementType()
1066 && "Ptr must be a pointer to Val type!");
1067 assert(Ordering != NotAtomic &&
1068 "AtomicRMW instructions must be atomic!");
1069}
1070
1071AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1072 AtomicOrdering Ordering,
1073 SynchronizationScope SynchScope,
1074 Instruction *InsertBefore)
1075 : Instruction(Val->getType(), AtomicRMW,
1076 OperandTraits<AtomicRMWInst>::op_begin(this),
1077 OperandTraits<AtomicRMWInst>::operands(this),
1078 InsertBefore) {
1079 Init(Operation, Ptr, Val, Ordering, SynchScope);
1080}
1081
1082AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1083 AtomicOrdering Ordering,
1084 SynchronizationScope SynchScope,
1085 BasicBlock *InsertAtEnd)
1086 : Instruction(Val->getType(), AtomicRMW,
1087 OperandTraits<AtomicRMWInst>::op_begin(this),
1088 OperandTraits<AtomicRMWInst>::operands(this),
1089 InsertAtEnd) {
1090 Init(Operation, Ptr, Val, Ordering, SynchScope);
1091}
1092
1093//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001094// FenceInst Implementation
1095//===----------------------------------------------------------------------===//
1096
1097FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1098 SynchronizationScope SynchScope,
1099 Instruction *InsertBefore)
1100 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1101 setOrdering(Ordering);
1102 setSynchScope(SynchScope);
1103}
1104
1105FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1106 SynchronizationScope SynchScope,
1107 BasicBlock *InsertAtEnd)
1108 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1109 setOrdering(Ordering);
1110 setSynchScope(SynchScope);
1111}
1112
1113//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001114// GetElementPtrInst Implementation
1115//===----------------------------------------------------------------------===//
1116
Jay Foadd1b78492011-07-25 09:48:08 +00001117void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001118 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001119 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1120 OperandList[0] = Ptr;
1121 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001122 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001123}
1124
Gabor Greiff6caff662008-05-10 08:32:32 +00001125GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001126 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001127 OperandTraits<GetElementPtrInst>::op_end(this)
1128 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001129 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001130 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001131 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001132}
1133
Chris Lattner6090a422009-03-09 04:46:40 +00001134/// getIndexedType - Returns the type of the element that would be accessed with
1135/// a gep instruction with the specified parameters.
1136///
1137/// The Idxs pointer should point to a continuous piece of memory containing the
1138/// indices, either as Value* or uint64_t.
1139///
1140/// A null type is returned if the indices are invalid for the specified
1141/// pointer type.
1142///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001143template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001144static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Chris Lattner229907c2011-07-18 04:54:35 +00001145 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001146 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001147 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001148
Chris Lattner6090a422009-03-09 04:46:40 +00001149 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001150 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001151 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001152
1153 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001154 // it cannot be 'stepped over'.
1155 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001156 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001157
Dan Gohman1ecaf452008-05-31 00:58:22 +00001158 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001159 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001160 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001161 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001162 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001163 if (!CT->indexValid(Index)) return 0;
1164 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001165 }
Jay Foadd1b78492011-07-25 09:48:08 +00001166 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001167}
1168
Jay Foadd1b78492011-07-25 09:48:08 +00001169Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1170 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001171}
1172
Chris Lattner229907c2011-07-18 04:54:35 +00001173Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001174 ArrayRef<Constant *> IdxList) {
1175 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001176}
1177
Jay Foadd1b78492011-07-25 09:48:08 +00001178Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1179 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001180}
1181
Chris Lattner45f15572007-04-14 00:12:57 +00001182/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1183/// zeros. If so, the result pointer and the first operand have the same
1184/// value, just potentially different types.
1185bool GetElementPtrInst::hasAllZeroIndices() const {
1186 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1187 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1188 if (!CI->isZero()) return false;
1189 } else {
1190 return false;
1191 }
1192 }
1193 return true;
1194}
1195
Chris Lattner27058292007-04-27 20:35:56 +00001196/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1197/// constant integers. If so, the result pointer and the first operand have
1198/// a constant offset between them.
1199bool GetElementPtrInst::hasAllConstantIndices() const {
1200 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1201 if (!isa<ConstantInt>(getOperand(i)))
1202 return false;
1203 }
1204 return true;
1205}
1206
Dan Gohman1b849082009-09-07 23:54:19 +00001207void GetElementPtrInst::setIsInBounds(bool B) {
1208 cast<GEPOperator>(this)->setIsInBounds(B);
1209}
Chris Lattner45f15572007-04-14 00:12:57 +00001210
Nick Lewycky28a5f252009-09-27 21:33:04 +00001211bool GetElementPtrInst::isInBounds() const {
1212 return cast<GEPOperator>(this)->isInBounds();
1213}
1214
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001215//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001216// ExtractElementInst Implementation
1217//===----------------------------------------------------------------------===//
1218
1219ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001220 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001221 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001222 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001223 ExtractElement,
1224 OperandTraits<ExtractElementInst>::op_begin(this),
1225 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001226 assert(isValidOperands(Val, Index) &&
1227 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001228 Op<0>() = Val;
1229 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001230 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001231}
1232
1233ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001234 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001235 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001236 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001237 ExtractElement,
1238 OperandTraits<ExtractElementInst>::op_begin(this),
1239 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001240 assert(isValidOperands(Val, Index) &&
1241 "Invalid extractelement instruction operands!");
1242
Gabor Greif2d3024d2008-05-26 21:33:52 +00001243 Op<0>() = Val;
1244 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001245 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001246}
1247
Chris Lattner65511ff2006-10-05 06:24:58 +00001248
Chris Lattner54865b32006-04-08 04:05:48 +00001249bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001250 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001251 return false;
1252 return true;
1253}
1254
1255
Robert Bocchino23004482006-01-10 19:05:34 +00001256//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001257// InsertElementInst Implementation
1258//===----------------------------------------------------------------------===//
1259
Chris Lattner54865b32006-04-08 04:05:48 +00001260InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001261 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001262 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001263 : Instruction(Vec->getType(), InsertElement,
1264 OperandTraits<InsertElementInst>::op_begin(this),
1265 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001266 assert(isValidOperands(Vec, Elt, Index) &&
1267 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001268 Op<0>() = Vec;
1269 Op<1>() = Elt;
1270 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001271 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001272}
1273
Chris Lattner54865b32006-04-08 04:05:48 +00001274InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001275 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001276 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001277 : Instruction(Vec->getType(), InsertElement,
1278 OperandTraits<InsertElementInst>::op_begin(this),
1279 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001280 assert(isValidOperands(Vec, Elt, Index) &&
1281 "Invalid insertelement instruction operands!");
1282
Gabor Greif2d3024d2008-05-26 21:33:52 +00001283 Op<0>() = Vec;
1284 Op<1>() = Elt;
1285 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001286 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001287}
1288
Chris Lattner54865b32006-04-08 04:05:48 +00001289bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1290 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001291 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001292 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001293
Reid Spencerd84d35b2007-02-15 02:26:10 +00001294 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001295 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001296
Duncan Sands9dff9be2010-02-15 16:12:20 +00001297 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001298 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001299 return true;
1300}
1301
1302
Robert Bocchinoca27f032006-01-17 20:07:22 +00001303//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001304// ShuffleVectorInst Implementation
1305//===----------------------------------------------------------------------===//
1306
1307ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001308 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001309 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001310: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001311 cast<VectorType>(Mask->getType())->getNumElements()),
1312 ShuffleVector,
1313 OperandTraits<ShuffleVectorInst>::op_begin(this),
1314 OperandTraits<ShuffleVectorInst>::operands(this),
1315 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001316 assert(isValidOperands(V1, V2, Mask) &&
1317 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001318 Op<0>() = V1;
1319 Op<1>() = V2;
1320 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001321 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001322}
1323
1324ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001325 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001326 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001327: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1328 cast<VectorType>(Mask->getType())->getNumElements()),
1329 ShuffleVector,
1330 OperandTraits<ShuffleVectorInst>::op_begin(this),
1331 OperandTraits<ShuffleVectorInst>::operands(this),
1332 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001333 assert(isValidOperands(V1, V2, Mask) &&
1334 "Invalid shuffle vector instruction operands!");
1335
Gabor Greif2d3024d2008-05-26 21:33:52 +00001336 Op<0>() = V1;
1337 Op<1>() = V2;
1338 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001339 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001340}
1341
Mon P Wang25f01062008-11-10 04:46:22 +00001342bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001343 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001344 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001345 return false;
1346
Chris Lattner229907c2011-07-18 04:54:35 +00001347 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001348 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001349 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001350
1351 // Check to see if Mask is valid.
1352 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001353 VectorType *VTy = cast<VectorType>(V1->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001354 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1355 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1356 if (CI->uge(VTy->getNumElements()*2))
1357 return false;
1358 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1359 return false;
1360 }
1361 }
1362 }
1363 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1364 return false;
1365
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001366 return true;
1367}
1368
Chris Lattnerf724e342008-03-02 05:28:33 +00001369/// getMaskValue - Return the index from the shuffle mask for the specified
1370/// output result. This is either -1 if the element is undef or a number less
1371/// than 2*numelements.
1372int ShuffleVectorInst::getMaskValue(unsigned i) const {
1373 const Constant *Mask = cast<Constant>(getOperand(2));
1374 if (isa<UndefValue>(Mask)) return -1;
1375 if (isa<ConstantAggregateZero>(Mask)) return 0;
1376 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1377 assert(i < MaskCV->getNumOperands() && "Index out of range");
1378
1379 if (isa<UndefValue>(MaskCV->getOperand(i)))
1380 return -1;
1381 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1382}
1383
Dan Gohman12fce772008-05-15 19:50:34 +00001384//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001385// InsertValueInst Class
1386//===----------------------------------------------------------------------===//
1387
Jay Foad57aa6362011-07-13 10:26:04 +00001388void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1389 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001390 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001391
1392 // There's no fundamental reason why we require at least one index
1393 // (other than weirdness with &*IdxBegin being invalid; see
1394 // getelementptr's init routine for example). But there's no
1395 // present need to support it.
1396 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1397
1398 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001399 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001400 Op<0>() = Agg;
1401 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001402
Jay Foad57aa6362011-07-13 10:26:04 +00001403 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001404 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001405}
1406
1407InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001408 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001409 OperandTraits<InsertValueInst>::op_begin(this), 2),
1410 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001411 Op<0>() = IVI.getOperand(0);
1412 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001413 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001414}
1415
1416//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001417// ExtractValueInst Class
1418//===----------------------------------------------------------------------===//
1419
Jay Foad57aa6362011-07-13 10:26:04 +00001420void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001421 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001422
Jay Foad57aa6362011-07-13 10:26:04 +00001423 // There's no fundamental reason why we require at least one index.
1424 // But there's no present need to support it.
1425 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001426
Jay Foad57aa6362011-07-13 10:26:04 +00001427 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001428 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001429}
1430
1431ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001432 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001433 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001434 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001435}
1436
Dan Gohman12fce772008-05-15 19:50:34 +00001437// getIndexedType - Returns the type of the element that would be extracted
1438// with an extractvalue instruction with the specified parameters.
1439//
1440// A null type is returned if the indices are invalid for the specified
1441// pointer type.
1442//
Chris Lattner229907c2011-07-18 04:54:35 +00001443Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001444 ArrayRef<unsigned> Idxs) {
1445 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001446 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001447 // We can't use CompositeType::indexValid(Index) here.
1448 // indexValid() always returns true for arrays because getelementptr allows
1449 // out-of-bounds indices. Since we don't allow those for extractvalue and
1450 // insertvalue we need to check array indexing manually.
1451 // Since the only other types we can index into are struct types it's just
1452 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001453 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001454 if (Index >= AT->getNumElements())
1455 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001456 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001457 if (Index >= ST->getNumElements())
1458 return 0;
1459 } else {
1460 // Not a valid type to index into.
1461 return 0;
1462 }
1463
1464 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001465 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001466 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001467}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001468
1469//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001470// BinaryOperator Class
1471//===----------------------------------------------------------------------===//
1472
Chris Lattner2195fc42007-02-24 00:55:48 +00001473BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001474 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001475 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001476 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001477 OperandTraits<BinaryOperator>::op_begin(this),
1478 OperandTraits<BinaryOperator>::operands(this),
1479 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001480 Op<0>() = S1;
1481 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001482 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001483 setName(Name);
1484}
1485
1486BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001487 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001488 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001489 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001490 OperandTraits<BinaryOperator>::op_begin(this),
1491 OperandTraits<BinaryOperator>::operands(this),
1492 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001493 Op<0>() = S1;
1494 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001495 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001496 setName(Name);
1497}
1498
1499
1500void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001501 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001502 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001503 assert(LHS->getType() == RHS->getType() &&
1504 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001505#ifndef NDEBUG
1506 switch (iType) {
1507 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001508 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001509 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001510 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001511 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001512 "Tried to create an integer operation on a non-integer type!");
1513 break;
1514 case FAdd: case FSub:
1515 case FMul:
1516 assert(getType() == LHS->getType() &&
1517 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001518 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001519 "Tried to create a floating-point operation on a "
1520 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001521 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001522 case UDiv:
1523 case SDiv:
1524 assert(getType() == LHS->getType() &&
1525 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001526 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001527 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001528 "Incorrect operand type (not integer) for S/UDIV");
1529 break;
1530 case FDiv:
1531 assert(getType() == LHS->getType() &&
1532 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001533 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001534 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001535 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001536 case URem:
1537 case SRem:
1538 assert(getType() == LHS->getType() &&
1539 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001540 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001541 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001542 "Incorrect operand type (not integer) for S/UREM");
1543 break;
1544 case FRem:
1545 assert(getType() == LHS->getType() &&
1546 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001547 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001548 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001549 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001550 case Shl:
1551 case LShr:
1552 case AShr:
1553 assert(getType() == LHS->getType() &&
1554 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001555 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001556 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001557 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001558 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001559 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001560 case And: case Or:
1561 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001562 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001563 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001564 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001565 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001566 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001567 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001568 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 default:
1570 break;
1571 }
1572#endif
1573}
1574
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001575BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001576 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001577 Instruction *InsertBefore) {
1578 assert(S1->getType() == S2->getType() &&
1579 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001580 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001581}
1582
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001583BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001584 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001585 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001586 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001587 InsertAtEnd->getInstList().push_back(Res);
1588 return Res;
1589}
1590
Dan Gohman5476cfd2009-08-12 16:23:25 +00001591BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001592 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001593 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001594 return new BinaryOperator(Instruction::Sub,
1595 zero, Op,
1596 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597}
1598
Dan Gohman5476cfd2009-08-12 16:23:25 +00001599BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001600 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001601 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001602 return new BinaryOperator(Instruction::Sub,
1603 zero, Op,
1604 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001605}
1606
Dan Gohman4ab44202009-12-18 02:58:50 +00001607BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1608 Instruction *InsertBefore) {
1609 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1610 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1611}
1612
1613BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1614 BasicBlock *InsertAtEnd) {
1615 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1616 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1617}
1618
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001619BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1620 Instruction *InsertBefore) {
1621 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1622 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1623}
1624
1625BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1626 BasicBlock *InsertAtEnd) {
1627 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1628 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1629}
1630
Dan Gohman5476cfd2009-08-12 16:23:25 +00001631BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001632 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001633 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001634 return new BinaryOperator(Instruction::FSub,
1635 zero, Op,
1636 Op->getType(), Name, InsertBefore);
1637}
1638
Dan Gohman5476cfd2009-08-12 16:23:25 +00001639BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001640 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001641 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001642 return new BinaryOperator(Instruction::FSub,
1643 zero, Op,
1644 Op->getType(), Name, InsertAtEnd);
1645}
1646
Dan Gohman5476cfd2009-08-12 16:23:25 +00001647BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001648 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001649 Constant *C;
Chris Lattner229907c2011-07-18 04:54:35 +00001650 if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001651 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001652 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001653 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001654 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001655 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001656 }
1657
1658 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001659 Op->getType(), Name, InsertBefore);
1660}
1661
Dan Gohman5476cfd2009-08-12 16:23:25 +00001662BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001663 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001664 Constant *AllOnes;
Chris Lattner229907c2011-07-18 04:54:35 +00001665 if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001666 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001667 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001668 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001669 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001670 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001671 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001672 }
1673
1674 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001675 Op->getType(), Name, InsertAtEnd);
1676}
1677
1678
1679// isConstantAllOnes - Helper function for several functions below
1680static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001681 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1682 return CI->isAllOnesValue();
1683 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1684 return CV->isAllOnesValue();
1685 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001686}
1687
Owen Andersonbb2501b2009-07-13 22:18:28 +00001688bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1690 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001691 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1692 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001693 return false;
1694}
1695
Owen Andersonbb2501b2009-07-13 22:18:28 +00001696bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001697 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1698 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001699 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001700 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001701 return false;
1702}
1703
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001704bool BinaryOperator::isNot(const Value *V) {
1705 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1706 return (Bop->getOpcode() == Instruction::Xor &&
1707 (isConstantAllOnes(Bop->getOperand(1)) ||
1708 isConstantAllOnes(Bop->getOperand(0))));
1709 return false;
1710}
1711
Chris Lattner2c7d1772005-04-24 07:28:37 +00001712Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001713 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001714}
1715
Chris Lattner2c7d1772005-04-24 07:28:37 +00001716const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1717 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001718}
1719
Dan Gohmana5b96452009-06-04 22:49:04 +00001720Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001721 return cast<BinaryOperator>(BinOp)->getOperand(1);
1722}
1723
1724const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1725 return getFNegArgument(const_cast<Value*>(BinOp));
1726}
1727
Chris Lattner2c7d1772005-04-24 07:28:37 +00001728Value *BinaryOperator::getNotArgument(Value *BinOp) {
1729 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1730 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1731 Value *Op0 = BO->getOperand(0);
1732 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001733 if (isConstantAllOnes(Op0)) return Op1;
1734
1735 assert(isConstantAllOnes(Op1));
1736 return Op0;
1737}
1738
Chris Lattner2c7d1772005-04-24 07:28:37 +00001739const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1740 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001741}
1742
1743
1744// swapOperands - Exchange the two operands to this instruction. This
1745// instruction is safe to use on any binary instruction and does not
1746// modify the semantics of the instruction. If the instruction is
1747// order dependent (SetLT f.e.) the opcode is changed.
1748//
1749bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001750 if (!isCommutative())
1751 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001752 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001753 return false;
1754}
1755
Dan Gohman1b849082009-09-07 23:54:19 +00001756void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1757 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1758}
1759
1760void BinaryOperator::setHasNoSignedWrap(bool b) {
1761 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1762}
1763
1764void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001765 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001766}
1767
Nick Lewycky28a5f252009-09-27 21:33:04 +00001768bool BinaryOperator::hasNoUnsignedWrap() const {
1769 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1770}
1771
1772bool BinaryOperator::hasNoSignedWrap() const {
1773 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1774}
1775
1776bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001777 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001778}
1779
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001780//===----------------------------------------------------------------------===//
1781// CastInst Class
1782//===----------------------------------------------------------------------===//
1783
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001784// Just determine if this cast only deals with integral->integral conversion.
1785bool CastInst::isIntegerCast() const {
1786 switch (getOpcode()) {
1787 default: return false;
1788 case Instruction::ZExt:
1789 case Instruction::SExt:
1790 case Instruction::Trunc:
1791 return true;
1792 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001793 return getOperand(0)->getType()->isIntegerTy() &&
1794 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001795 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001796}
1797
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001798bool CastInst::isLosslessCast() const {
1799 // Only BitCast can be lossless, exit fast if we're not BitCast
1800 if (getOpcode() != Instruction::BitCast)
1801 return false;
1802
1803 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00001804 Type* SrcTy = getOperand(0)->getType();
1805 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001806 if (SrcTy == DstTy)
1807 return true;
1808
Reid Spencer8d9336d2006-12-31 05:26:44 +00001809 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001810 if (SrcTy->isPointerTy())
1811 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001812 return false; // Other types have no identity values
1813}
1814
1815/// This function determines if the CastInst does not require any bits to be
1816/// changed in order to effect the cast. Essentially, it identifies cases where
1817/// no code gen is necessary for the cast, hence the name no-op cast. For
1818/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001819/// # bitcast i32* %x to i8*
1820/// # bitcast <2 x i32> %x to <4 x i16>
1821/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001822/// @brief Determine if the described cast is a no-op.
1823bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00001824 Type *SrcTy,
1825 Type *DestTy,
1826 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001827 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001828 default:
1829 assert(!"Invalid CastOp");
1830 case Instruction::Trunc:
1831 case Instruction::ZExt:
1832 case Instruction::SExt:
1833 case Instruction::FPTrunc:
1834 case Instruction::FPExt:
1835 case Instruction::UIToFP:
1836 case Instruction::SIToFP:
1837 case Instruction::FPToUI:
1838 case Instruction::FPToSI:
1839 return false; // These always modify bits
1840 case Instruction::BitCast:
1841 return true; // BitCast never modifies bits.
1842 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001843 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001844 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001845 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001846 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001847 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001848 }
1849}
1850
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001851/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00001852bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001853 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1854}
1855
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001856/// This function determines if a pair of casts can be eliminated and what
1857/// opcode should be used in the elimination. This assumes that there are two
1858/// instructions like this:
1859/// * %F = firstOpcode SrcTy %x to MidTy
1860/// * %S = secondOpcode MidTy %F to DstTy
1861/// The function returns a resultOpcode so these two casts can be replaced with:
1862/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1863/// If no such cast is permited, the function returns 0.
1864unsigned CastInst::isEliminableCastPair(
1865 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Chris Lattner229907c2011-07-18 04:54:35 +00001866 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001867{
1868 // Define the 144 possibilities for these two cast instructions. The values
1869 // in this matrix determine what to do in a given situation and select the
1870 // case in the switch below. The rows correspond to firstOp, the columns
1871 // correspond to secondOp. In looking at the table below, keep in mind
1872 // the following cast properties:
1873 //
1874 // Size Compare Source Destination
1875 // Operator Src ? Size Type Sign Type Sign
1876 // -------- ------------ ------------------- ---------------------
1877 // TRUNC > Integer Any Integral Any
1878 // ZEXT < Integral Unsigned Integer Any
1879 // SEXT < Integral Signed Integer Any
1880 // FPTOUI n/a FloatPt n/a Integral Unsigned
1881 // FPTOSI n/a FloatPt n/a Integral Signed
1882 // UITOFP n/a Integral Unsigned FloatPt n/a
1883 // SITOFP n/a Integral Signed FloatPt n/a
1884 // FPTRUNC > FloatPt n/a FloatPt n/a
1885 // FPEXT < FloatPt n/a FloatPt n/a
1886 // PTRTOINT n/a Pointer n/a Integral Unsigned
1887 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001888 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001889 //
1890 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001891 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1892 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001893 // of the produced value (we no longer know the top-part is all zeros).
1894 // Further this conversion is often much more expensive for typical hardware,
1895 // and causes issues when building libgcc. We disallow fptosi+sext for the
1896 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001897 const unsigned numCastOps =
1898 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1899 static const uint8_t CastResults[numCastOps][numCastOps] = {
1900 // T F F U S F F P I B -+
1901 // R Z S P P I I T P 2 N T |
1902 // U E E 2 2 2 2 R E I T C +- secondOp
1903 // N X X U S F F N X N 2 V |
1904 // C T T I I P P C T T P T -+
1905 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1906 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1907 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001908 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1909 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001910 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1911 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1912 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1913 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1914 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1915 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1916 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1917 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00001918
1919 // If either of the casts are a bitcast from scalar to vector, disallow the
1920 // merging.
1921 if ((firstOp == Instruction::BitCast &&
1922 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
1923 (secondOp == Instruction::BitCast &&
1924 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
1925 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001926
1927 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1928 [secondOp-Instruction::CastOpsBegin];
1929 switch (ElimCase) {
1930 case 0:
1931 // categorically disallowed
1932 return 0;
1933 case 1:
1934 // allowed, use first cast's opcode
1935 return firstOp;
1936 case 2:
1937 // allowed, use second cast's opcode
1938 return secondOp;
1939 case 3:
1940 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00001941 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00001942 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001943 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001944 return firstOp;
1945 return 0;
1946 case 4:
1947 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00001948 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001949 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001950 return firstOp;
1951 return 0;
1952 case 5:
1953 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00001954 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001955 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956 return secondOp;
1957 return 0;
1958 case 6:
1959 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00001960 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001961 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001962 return secondOp;
1963 return 0;
1964 case 7: {
1965 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00001966 if (!IntPtrTy)
1967 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001968 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1969 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001970 if (MidSize >= PtrSize)
1971 return Instruction::BitCast;
1972 return 0;
1973 }
1974 case 8: {
1975 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1976 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1977 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001978 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1979 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001980 if (SrcSize == DstSize)
1981 return Instruction::BitCast;
1982 else if (SrcSize < DstSize)
1983 return firstOp;
1984 return secondOp;
1985 }
1986 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1987 return Instruction::ZExt;
1988 case 10:
1989 // fpext followed by ftrunc is allowed if the bit size returned to is
1990 // the same as the original, in which case its just a bitcast
1991 if (SrcTy == DstTy)
1992 return Instruction::BitCast;
1993 return 0; // If the types are not the same we can't eliminate it.
1994 case 11:
1995 // bitcast followed by ptrtoint is allowed as long as the bitcast
1996 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00001997 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001998 return secondOp;
1999 return 0;
2000 case 12:
2001 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002002 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003 return firstOp;
2004 return 0;
2005 case 13: {
2006 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002007 if (!IntPtrTy)
2008 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002009 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2010 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2011 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002012 if (SrcSize <= PtrSize && SrcSize == DstSize)
2013 return Instruction::BitCast;
2014 return 0;
2015 }
2016 case 99:
2017 // cast combination can't happen (error in input). This is for all cases
2018 // where the MidTy is not the same for the two cast instructions.
2019 assert(!"Invalid Cast Combination");
2020 return 0;
2021 default:
2022 assert(!"Error in CastResults table!!!");
2023 return 0;
2024 }
2025 return 0;
2026}
2027
Chris Lattner229907c2011-07-18 04:54:35 +00002028CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002029 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002030 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002031 // Construct and return the appropriate CastInst subclass
2032 switch (op) {
2033 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2034 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2035 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2036 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2037 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2038 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2039 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2040 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2041 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2042 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2043 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2044 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2045 default:
2046 assert(!"Invalid opcode provided");
2047 }
2048 return 0;
2049}
2050
Chris Lattner229907c2011-07-18 04:54:35 +00002051CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002052 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002053 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002054 // Construct and return the appropriate CastInst subclass
2055 switch (op) {
2056 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2057 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2058 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2059 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2060 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2061 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2062 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2063 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2064 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2065 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2066 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2067 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2068 default:
2069 assert(!"Invalid opcode provided");
2070 }
2071 return 0;
2072}
2073
Chris Lattner229907c2011-07-18 04:54:35 +00002074CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002075 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002076 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002077 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002078 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2079 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002080}
2081
Chris Lattner229907c2011-07-18 04:54:35 +00002082CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002083 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002084 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002085 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002086 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2087 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002088}
2089
Chris Lattner229907c2011-07-18 04:54:35 +00002090CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002091 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002092 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002093 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002094 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2095 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002096}
2097
Chris Lattner229907c2011-07-18 04:54:35 +00002098CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002099 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002100 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002101 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002102 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2103 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002104}
2105
Chris Lattner229907c2011-07-18 04:54:35 +00002106CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002107 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002108 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002109 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002110 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2111 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002112}
2113
Chris Lattner229907c2011-07-18 04:54:35 +00002114CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002115 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002116 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002117 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002118 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2119 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002120}
2121
Chris Lattner229907c2011-07-18 04:54:35 +00002122CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002123 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002124 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002125 assert(S->getType()->isPointerTy() && "Invalid cast");
2126 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002127 "Invalid cast");
2128
Duncan Sands9dff9be2010-02-15 16:12:20 +00002129 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002130 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2131 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002132}
2133
2134/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002135CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002136 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002137 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002138 assert(S->getType()->isPointerTy() && "Invalid cast");
2139 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002140 "Invalid cast");
2141
Duncan Sands9dff9be2010-02-15 16:12:20 +00002142 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002143 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2144 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002145}
2146
Chris Lattner229907c2011-07-18 04:54:35 +00002147CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002148 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002149 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002150 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002151 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002152 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2153 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002154 Instruction::CastOps opcode =
2155 (SrcBits == DstBits ? Instruction::BitCast :
2156 (SrcBits > DstBits ? Instruction::Trunc :
2157 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002158 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002159}
2160
Chris Lattner229907c2011-07-18 04:54:35 +00002161CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002162 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002163 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002164 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002165 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002166 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2167 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002168 Instruction::CastOps opcode =
2169 (SrcBits == DstBits ? Instruction::BitCast :
2170 (SrcBits > DstBits ? Instruction::Trunc :
2171 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002172 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002173}
2174
Chris Lattner229907c2011-07-18 04:54:35 +00002175CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002176 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002177 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002178 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002179 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002180 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2181 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002182 Instruction::CastOps opcode =
2183 (SrcBits == DstBits ? Instruction::BitCast :
2184 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002185 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002186}
2187
Chris Lattner229907c2011-07-18 04:54:35 +00002188CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002189 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002190 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002191 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002192 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002193 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2194 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002195 Instruction::CastOps opcode =
2196 (SrcBits == DstBits ? Instruction::BitCast :
2197 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002198 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002199}
2200
Duncan Sands55e50902008-01-06 10:12:28 +00002201// Check whether it is valid to call getCastOpcode for these types.
2202// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002203bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002204 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2205 return false;
2206
2207 if (SrcTy == DestTy)
2208 return true;
2209
Chris Lattner229907c2011-07-18 04:54:35 +00002210 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2211 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002212 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2213 // An element by element cast. Valid if casting the elements is valid.
2214 SrcTy = SrcVecTy->getElementType();
2215 DestTy = DestVecTy->getElementType();
2216 }
2217
Duncan Sands55e50902008-01-06 10:12:28 +00002218 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002219 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2220 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002221
2222 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002223 if (DestTy->isIntegerTy()) { // Casting to integral
2224 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002225 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002226 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002227 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002228 } else if (SrcTy->isVectorTy()) { // Casting from vector
2229 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002230 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002231 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002232 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002233 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2234 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002235 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002236 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002237 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002238 } else if (SrcTy->isVectorTy()) { // Casting from vector
2239 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002240 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002241 return false;
2242 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002243 } else if (DestTy->isVectorTy()) { // Casting to vector
2244 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002245 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002246 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002247 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002248 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002249 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002250 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002251 return false;
2252 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002253 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002254 if (SrcTy->isVectorTy()) {
2255 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002256 } else {
2257 return false;
2258 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002259 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002260 return false;
2261 }
2262}
2263
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264// Provide a way to get a "cast" where the cast opcode is inferred from the
2265// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002266// logic in the castIsValid function below. This axiom should hold:
2267// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2268// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002269// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002270// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002272CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002273 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2274 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275
Duncan Sands55e50902008-01-06 10:12:28 +00002276 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2277 "Only first class types are castable!");
2278
Duncan Sandsa8514532011-05-18 07:13:41 +00002279 if (SrcTy == DestTy)
2280 return BitCast;
2281
Chris Lattner229907c2011-07-18 04:54:35 +00002282 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2283 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002284 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2285 // An element by element cast. Find the appropriate opcode based on the
2286 // element types.
2287 SrcTy = SrcVecTy->getElementType();
2288 DestTy = DestVecTy->getElementType();
2289 }
2290
2291 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002292 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2293 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002294
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002296 if (DestTy->isIntegerTy()) { // Casting to integral
2297 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002298 if (DestBits < SrcBits)
2299 return Trunc; // int -> smaller int
2300 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002301 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302 return SExt; // signed -> SEXT
2303 else
2304 return ZExt; // unsigned -> ZEXT
2305 } else {
2306 return BitCast; // Same size, No-op cast
2307 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002308 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002309 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 return FPToSI; // FP -> sint
2311 else
2312 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002313 } else if (SrcTy->isVectorTy()) {
2314 assert(DestBits == SrcBits &&
2315 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316 return BitCast; // Same size, no-op cast
2317 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002318 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319 "Casting from a value that is not first-class type");
2320 return PtrToInt; // ptr -> int
2321 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002322 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2323 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002324 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002325 return SIToFP; // sint -> FP
2326 else
2327 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002328 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 if (DestBits < SrcBits) {
2330 return FPTrunc; // FP -> smaller FP
2331 } else if (DestBits > SrcBits) {
2332 return FPExt; // FP -> larger FP
2333 } else {
2334 return BitCast; // same size, no-op cast
2335 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002336 } else if (SrcTy->isVectorTy()) {
2337 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002338 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002339 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002341 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002343 } else if (DestTy->isVectorTy()) {
2344 assert(DestBits == SrcBits &&
2345 "Illegal cast to vector (wrong type or size)");
2346 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002347 } else if (DestTy->isPointerTy()) {
2348 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002350 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351 return IntToPtr; // int -> ptr
2352 } else {
2353 assert(!"Casting pointer to other than pointer or int");
2354 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002355 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002356 if (SrcTy->isVectorTy()) {
2357 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002358 return BitCast; // 64-bit vector to MMX
2359 } else {
2360 assert(!"Illegal cast to X86_MMX");
2361 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362 } else {
2363 assert(!"Casting to type that is not first-class");
2364 }
2365
2366 // If we fall through to here we probably hit an assertion cast above
2367 // and assertions are not turned on. Anything we return is an error, so
2368 // BitCast is as good a choice as any.
2369 return BitCast;
2370}
2371
2372//===----------------------------------------------------------------------===//
2373// CastInst SubClass Constructors
2374//===----------------------------------------------------------------------===//
2375
2376/// Check that the construction parameters for a CastInst are correct. This
2377/// could be broken out into the separate constructors but it is useful to have
2378/// it in one place and to eliminate the redundant code for getting the sizes
2379/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002380bool
Chris Lattner229907c2011-07-18 04:54:35 +00002381CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382
2383 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002384 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002385 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2386 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387 return false;
2388
2389 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002390 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2391 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392
Duncan Sands7f646562011-05-18 09:21:57 +00002393 // If these are vector types, get the lengths of the vectors (using zero for
2394 // scalar types means that checking that vector lengths match also checks that
2395 // scalars are not being converted to vectors or vectors to scalars).
2396 unsigned SrcLength = SrcTy->isVectorTy() ?
2397 cast<VectorType>(SrcTy)->getNumElements() : 0;
2398 unsigned DstLength = DstTy->isVectorTy() ?
2399 cast<VectorType>(DstTy)->getNumElements() : 0;
2400
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 // Switch on the opcode provided
2402 switch (op) {
2403 default: return false; // This is an input error
2404 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002405 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2406 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002408 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2409 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002411 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2412 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002414 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2415 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002417 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2418 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002421 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2422 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002425 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2426 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002428 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002430 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431 case Instruction::BitCast:
2432 // BitCast implies a no-op cast of type only. No bits change.
2433 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002434 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 return false;
2436
Duncan Sands55e50902008-01-06 10:12:28 +00002437 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438 // these cases, the cast is okay if the source and destination bit widths
2439 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002440 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441 }
2442}
2443
2444TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002445 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002447 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002448}
2449
2450TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002451 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002453 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454}
2455
2456ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002457 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002459 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460}
2461
2462ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002463 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002465 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002466}
2467SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002468 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002470 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471}
2472
Jeff Cohencc08c832006-12-02 02:22:01 +00002473SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002474 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002476 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002477}
2478
2479FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002480 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002482 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483}
2484
2485FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002486 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002488 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002489}
2490
2491FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002492 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002494 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495}
2496
2497FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002498 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002500 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501}
2502
2503UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002504 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002506 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507}
2508
2509UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002510 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002512 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513}
2514
2515SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002516 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002518 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519}
2520
2521SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002522 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002523) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002524 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525}
2526
2527FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002528 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002530 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531}
2532
2533FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002534 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002536 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537}
2538
2539FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002540 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002542 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543}
2544
2545FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002546 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002548 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549}
2550
2551PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002552 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002554 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555}
2556
2557PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002558 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002560 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561}
2562
2563IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002564 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002566 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002567}
2568
2569IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002570 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002572 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573}
2574
2575BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002576 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002578 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579}
2580
2581BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002582 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002584 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002586
2587//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002588// CmpInst Classes
2589//===----------------------------------------------------------------------===//
2590
Chris Lattneraec33da2010-01-22 06:25:37 +00002591void CmpInst::Anchor() const {}
2592
Chris Lattner229907c2011-07-18 04:54:35 +00002593CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002594 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002595 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002596 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002597 OperandTraits<CmpInst>::op_begin(this),
2598 OperandTraits<CmpInst>::operands(this),
2599 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002600 Op<0>() = LHS;
2601 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002602 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002603 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002604}
Gabor Greiff6caff662008-05-10 08:32:32 +00002605
Chris Lattner229907c2011-07-18 04:54:35 +00002606CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002607 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002608 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002609 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002610 OperandTraits<CmpInst>::op_begin(this),
2611 OperandTraits<CmpInst>::operands(this),
2612 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002613 Op<0>() = LHS;
2614 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002615 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002616 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002617}
2618
2619CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002620CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002621 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002622 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002623 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002624 if (InsertBefore)
2625 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2626 S1, S2, Name);
2627 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002628 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002629 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002630 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002631
2632 if (InsertBefore)
2633 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2634 S1, S2, Name);
2635 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002636 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002637 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002638}
2639
2640CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002641CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002642 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002643 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002644 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2645 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002646 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002647 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2648 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002649}
2650
2651void CmpInst::swapOperands() {
2652 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2653 IC->swapOperands();
2654 else
2655 cast<FCmpInst>(this)->swapOperands();
2656}
2657
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002658bool CmpInst::isCommutative() const {
2659 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002660 return IC->isCommutative();
2661 return cast<FCmpInst>(this)->isCommutative();
2662}
2663
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002664bool CmpInst::isEquality() const {
2665 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002666 return IC->isEquality();
2667 return cast<FCmpInst>(this)->isEquality();
2668}
2669
2670
Dan Gohman4e724382008-05-31 02:47:54 +00002671CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002672 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002673 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002674 case ICMP_EQ: return ICMP_NE;
2675 case ICMP_NE: return ICMP_EQ;
2676 case ICMP_UGT: return ICMP_ULE;
2677 case ICMP_ULT: return ICMP_UGE;
2678 case ICMP_UGE: return ICMP_ULT;
2679 case ICMP_ULE: return ICMP_UGT;
2680 case ICMP_SGT: return ICMP_SLE;
2681 case ICMP_SLT: return ICMP_SGE;
2682 case ICMP_SGE: return ICMP_SLT;
2683 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002684
Dan Gohman4e724382008-05-31 02:47:54 +00002685 case FCMP_OEQ: return FCMP_UNE;
2686 case FCMP_ONE: return FCMP_UEQ;
2687 case FCMP_OGT: return FCMP_ULE;
2688 case FCMP_OLT: return FCMP_UGE;
2689 case FCMP_OGE: return FCMP_ULT;
2690 case FCMP_OLE: return FCMP_UGT;
2691 case FCMP_UEQ: return FCMP_ONE;
2692 case FCMP_UNE: return FCMP_OEQ;
2693 case FCMP_UGT: return FCMP_OLE;
2694 case FCMP_ULT: return FCMP_OGE;
2695 case FCMP_UGE: return FCMP_OLT;
2696 case FCMP_ULE: return FCMP_OGT;
2697 case FCMP_ORD: return FCMP_UNO;
2698 case FCMP_UNO: return FCMP_ORD;
2699 case FCMP_TRUE: return FCMP_FALSE;
2700 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002701 }
2702}
2703
Reid Spencer266e42b2006-12-23 06:05:41 +00002704ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2705 switch (pred) {
2706 default: assert(! "Unknown icmp predicate!");
2707 case ICMP_EQ: case ICMP_NE:
2708 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2709 return pred;
2710 case ICMP_UGT: return ICMP_SGT;
2711 case ICMP_ULT: return ICMP_SLT;
2712 case ICMP_UGE: return ICMP_SGE;
2713 case ICMP_ULE: return ICMP_SLE;
2714 }
2715}
2716
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002717ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2718 switch (pred) {
2719 default: assert(! "Unknown icmp predicate!");
2720 case ICMP_EQ: case ICMP_NE:
2721 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2722 return pred;
2723 case ICMP_SGT: return ICMP_UGT;
2724 case ICMP_SLT: return ICMP_ULT;
2725 case ICMP_SGE: return ICMP_UGE;
2726 case ICMP_SLE: return ICMP_ULE;
2727 }
2728}
2729
Reid Spencer0286bc12007-02-28 22:00:54 +00002730/// Initialize a set of values that all satisfy the condition with C.
2731///
2732ConstantRange
2733ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2734 APInt Lower(C);
2735 APInt Upper(C);
2736 uint32_t BitWidth = C.getBitWidth();
2737 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002738 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002739 case ICmpInst::ICMP_EQ: Upper++; break;
2740 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002741 case ICmpInst::ICMP_ULT:
2742 Lower = APInt::getMinValue(BitWidth);
2743 // Check for an empty-set condition.
2744 if (Lower == Upper)
2745 return ConstantRange(BitWidth, /*isFullSet=*/false);
2746 break;
2747 case ICmpInst::ICMP_SLT:
2748 Lower = APInt::getSignedMinValue(BitWidth);
2749 // Check for an empty-set condition.
2750 if (Lower == Upper)
2751 return ConstantRange(BitWidth, /*isFullSet=*/false);
2752 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002753 case ICmpInst::ICMP_UGT:
2754 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002755 // Check for an empty-set condition.
2756 if (Lower == Upper)
2757 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002758 break;
2759 case ICmpInst::ICMP_SGT:
2760 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002761 // Check for an empty-set condition.
2762 if (Lower == Upper)
2763 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002764 break;
2765 case ICmpInst::ICMP_ULE:
2766 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002767 // Check for a full-set condition.
2768 if (Lower == Upper)
2769 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002770 break;
2771 case ICmpInst::ICMP_SLE:
2772 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002773 // Check for a full-set condition.
2774 if (Lower == Upper)
2775 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002776 break;
2777 case ICmpInst::ICMP_UGE:
2778 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002779 // Check for a full-set condition.
2780 if (Lower == Upper)
2781 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002782 break;
2783 case ICmpInst::ICMP_SGE:
2784 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002785 // Check for a full-set condition.
2786 if (Lower == Upper)
2787 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002788 break;
2789 }
2790 return ConstantRange(Lower, Upper);
2791}
2792
Dan Gohman4e724382008-05-31 02:47:54 +00002793CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002794 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002795 default: assert(!"Unknown cmp predicate!");
2796 case ICMP_EQ: case ICMP_NE:
2797 return pred;
2798 case ICMP_SGT: return ICMP_SLT;
2799 case ICMP_SLT: return ICMP_SGT;
2800 case ICMP_SGE: return ICMP_SLE;
2801 case ICMP_SLE: return ICMP_SGE;
2802 case ICMP_UGT: return ICMP_ULT;
2803 case ICMP_ULT: return ICMP_UGT;
2804 case ICMP_UGE: return ICMP_ULE;
2805 case ICMP_ULE: return ICMP_UGE;
2806
Reid Spencerd9436b62006-11-20 01:22:35 +00002807 case FCMP_FALSE: case FCMP_TRUE:
2808 case FCMP_OEQ: case FCMP_ONE:
2809 case FCMP_UEQ: case FCMP_UNE:
2810 case FCMP_ORD: case FCMP_UNO:
2811 return pred;
2812 case FCMP_OGT: return FCMP_OLT;
2813 case FCMP_OLT: return FCMP_OGT;
2814 case FCMP_OGE: return FCMP_OLE;
2815 case FCMP_OLE: return FCMP_OGE;
2816 case FCMP_UGT: return FCMP_ULT;
2817 case FCMP_ULT: return FCMP_UGT;
2818 case FCMP_UGE: return FCMP_ULE;
2819 case FCMP_ULE: return FCMP_UGE;
2820 }
2821}
2822
Reid Spencer266e42b2006-12-23 06:05:41 +00002823bool CmpInst::isUnsigned(unsigned short predicate) {
2824 switch (predicate) {
2825 default: return false;
2826 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2827 case ICmpInst::ICMP_UGE: return true;
2828 }
2829}
2830
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002831bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002832 switch (predicate) {
2833 default: return false;
2834 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2835 case ICmpInst::ICMP_SGE: return true;
2836 }
2837}
2838
2839bool CmpInst::isOrdered(unsigned short predicate) {
2840 switch (predicate) {
2841 default: return false;
2842 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2843 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2844 case FCmpInst::FCMP_ORD: return true;
2845 }
2846}
2847
2848bool CmpInst::isUnordered(unsigned short predicate) {
2849 switch (predicate) {
2850 default: return false;
2851 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2852 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2853 case FCmpInst::FCMP_UNO: return true;
2854 }
2855}
2856
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002857bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2858 switch(predicate) {
2859 default: return false;
2860 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2861 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2862 }
2863}
2864
2865bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2866 switch(predicate) {
2867 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2868 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2869 default: return false;
2870 }
2871}
2872
2873
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002874//===----------------------------------------------------------------------===//
2875// SwitchInst Implementation
2876//===----------------------------------------------------------------------===//
2877
Chris Lattnerbaf00152010-11-17 05:41:46 +00002878void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
2879 assert(Value && Default && NumReserved);
2880 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002881 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002882 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002883
Gabor Greif2d3024d2008-05-26 21:33:52 +00002884 OperandList[0] = Value;
2885 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002886}
2887
Chris Lattner2195fc42007-02-24 00:55:48 +00002888/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2889/// switch on and a default destination. The number of additional cases can
2890/// be specified here to make memory allocation more efficient. This
2891/// constructor can also autoinsert before another instruction.
2892SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2893 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002894 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2895 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002896 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002897}
2898
2899/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2900/// switch on and a default destination. The number of additional cases can
2901/// be specified here to make memory allocation more efficient. This
2902/// constructor also autoinserts at the end of the specified BasicBlock.
2903SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2904 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002905 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2906 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002907 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002908}
2909
Misha Brukmanb1c93172005-04-21 23:48:37 +00002910SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00002911 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
2912 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
2913 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002914 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00002915 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002916 OL[i] = InOL[i];
2917 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002918 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002919 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002920}
2921
Gordon Henriksen14a55692007-12-10 02:14:30 +00002922SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00002923 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002924}
2925
2926
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002927/// addCase - Add an entry to the switch instruction...
2928///
Chris Lattner47ac1872005-02-24 05:32:09 +00002929void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002930 unsigned OpNo = NumOperands;
2931 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00002932 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002933 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002934 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002935 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002936 OperandList[OpNo] = OnVal;
2937 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002938}
2939
2940/// removeCase - This method removes the specified successor from the switch
2941/// instruction. Note that this cannot be used to remove the default
2942/// destination (successor #0).
2943///
2944void SwitchInst::removeCase(unsigned idx) {
2945 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002946 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2947
2948 unsigned NumOps = getNumOperands();
2949 Use *OL = OperandList;
2950
Jay Foad14277722011-02-01 09:22:34 +00002951 // Overwrite this case with the end of the list.
2952 if ((idx + 1) * 2 != NumOps) {
2953 OL[idx * 2] = OL[NumOps - 2];
2954 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002955 }
2956
2957 // Nuke the last value.
2958 OL[NumOps-2].set(0);
2959 OL[NumOps-2+1].set(0);
2960 NumOperands = NumOps-2;
2961}
2962
Jay Foade98f29d2011-04-01 08:00:58 +00002963/// growOperands - grow operands - This grows the operand list in response
2964/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002965///
Jay Foade98f29d2011-04-01 08:00:58 +00002966void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002967 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00002968 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002969
2970 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002971 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002972 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002973 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002974 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002975 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002976 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00002977 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002978}
2979
2980
2981BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2982 return getSuccessor(idx);
2983}
2984unsigned SwitchInst::getNumSuccessorsV() const {
2985 return getNumSuccessors();
2986}
2987void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2988 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002989}
Chris Lattnerf22be932004-10-15 23:52:53 +00002990
Chris Lattner3ed871f2009-10-27 19:13:16 +00002991//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00002992// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00002993//===----------------------------------------------------------------------===//
2994
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002995void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002996 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00002997 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00002998 ReservedSpace = 1+NumDests;
2999 NumOperands = 1;
3000 OperandList = allocHungoffUses(ReservedSpace);
3001
3002 OperandList[0] = Address;
3003}
3004
3005
Jay Foade98f29d2011-04-01 08:00:58 +00003006/// growOperands - grow operands - This grows the operand list in response
3007/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003008///
Jay Foade98f29d2011-04-01 08:00:58 +00003009void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003010 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003011 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003012
3013 ReservedSpace = NumOps;
3014 Use *NewOps = allocHungoffUses(NumOps);
3015 Use *OldOps = OperandList;
3016 for (unsigned i = 0; i != e; ++i)
3017 NewOps[i] = OldOps[i];
3018 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003019 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003020}
3021
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003022IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3023 Instruction *InsertBefore)
3024: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003025 0, 0, InsertBefore) {
3026 init(Address, NumCases);
3027}
3028
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003029IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3030 BasicBlock *InsertAtEnd)
3031: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003032 0, 0, InsertAtEnd) {
3033 init(Address, NumCases);
3034}
3035
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003036IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3037 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003038 allocHungoffUses(IBI.getNumOperands()),
3039 IBI.getNumOperands()) {
3040 Use *OL = OperandList, *InOL = IBI.OperandList;
3041 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3042 OL[i] = InOL[i];
3043 SubclassOptionalData = IBI.SubclassOptionalData;
3044}
3045
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003046IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003047 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003048}
3049
3050/// addDestination - Add a destination.
3051///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003052void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003053 unsigned OpNo = NumOperands;
3054 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003055 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003056 // Initialize some new operands.
3057 assert(OpNo < ReservedSpace && "Growing didn't work!");
3058 NumOperands = OpNo+1;
3059 OperandList[OpNo] = DestBB;
3060}
3061
3062/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003063/// indirectbr instruction.
3064void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003065 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3066
3067 unsigned NumOps = getNumOperands();
3068 Use *OL = OperandList;
3069
3070 // Replace this value with the last one.
3071 OL[idx+1] = OL[NumOps-1];
3072
3073 // Nuke the last value.
3074 OL[NumOps-1].set(0);
3075 NumOperands = NumOps-1;
3076}
3077
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003078BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003079 return getSuccessor(idx);
3080}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003081unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003082 return getNumSuccessors();
3083}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003084void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003085 setSuccessor(idx, B);
3086}
3087
3088//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003089// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003090//===----------------------------------------------------------------------===//
3091
Chris Lattnerf22be932004-10-15 23:52:53 +00003092// Define these methods here so vtables don't get emitted into every translation
3093// unit that uses these classes.
3094
Devang Patel11cf3f42009-10-27 22:16:29 +00003095GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3096 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003097}
3098
Devang Patel11cf3f42009-10-27 22:16:29 +00003099BinaryOperator *BinaryOperator::clone_impl() const {
3100 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003101}
3102
Devang Patel11cf3f42009-10-27 22:16:29 +00003103FCmpInst* FCmpInst::clone_impl() const {
3104 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003105}
3106
Devang Patel11cf3f42009-10-27 22:16:29 +00003107ICmpInst* ICmpInst::clone_impl() const {
3108 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003109}
3110
Devang Patel11cf3f42009-10-27 22:16:29 +00003111ExtractValueInst *ExtractValueInst::clone_impl() const {
3112 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003113}
3114
Devang Patel11cf3f42009-10-27 22:16:29 +00003115InsertValueInst *InsertValueInst::clone_impl() const {
3116 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003117}
3118
Devang Patel11cf3f42009-10-27 22:16:29 +00003119AllocaInst *AllocaInst::clone_impl() const {
3120 return new AllocaInst(getAllocatedType(),
3121 (Value*)getOperand(0),
3122 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003123}
3124
Devang Patel11cf3f42009-10-27 22:16:29 +00003125LoadInst *LoadInst::clone_impl() const {
3126 return new LoadInst(getOperand(0),
3127 Twine(), isVolatile(),
3128 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003129}
3130
Devang Patel11cf3f42009-10-27 22:16:29 +00003131StoreInst *StoreInst::clone_impl() const {
3132 return new StoreInst(getOperand(0), getOperand(1),
3133 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003134}
3135
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003136AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3137 AtomicCmpXchgInst *Result =
3138 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3139 getOrdering(), getSynchScope());
3140 Result->setVolatile(isVolatile());
3141 return Result;
3142}
3143
3144AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3145 AtomicRMWInst *Result =
3146 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3147 getOrdering(), getSynchScope());
3148 Result->setVolatile(isVolatile());
3149 return Result;
3150}
3151
Eli Friedmanfee02c62011-07-25 23:16:38 +00003152FenceInst *FenceInst::clone_impl() const {
3153 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3154}
3155
Devang Patel11cf3f42009-10-27 22:16:29 +00003156TruncInst *TruncInst::clone_impl() const {
3157 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003158}
3159
Devang Patel11cf3f42009-10-27 22:16:29 +00003160ZExtInst *ZExtInst::clone_impl() const {
3161 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003162}
3163
Devang Patel11cf3f42009-10-27 22:16:29 +00003164SExtInst *SExtInst::clone_impl() const {
3165 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003166}
3167
Devang Patel11cf3f42009-10-27 22:16:29 +00003168FPTruncInst *FPTruncInst::clone_impl() const {
3169 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003170}
3171
Devang Patel11cf3f42009-10-27 22:16:29 +00003172FPExtInst *FPExtInst::clone_impl() const {
3173 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003174}
3175
Devang Patel11cf3f42009-10-27 22:16:29 +00003176UIToFPInst *UIToFPInst::clone_impl() const {
3177 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003178}
3179
Devang Patel11cf3f42009-10-27 22:16:29 +00003180SIToFPInst *SIToFPInst::clone_impl() const {
3181 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003182}
3183
Devang Patel11cf3f42009-10-27 22:16:29 +00003184FPToUIInst *FPToUIInst::clone_impl() const {
3185 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003186}
3187
Devang Patel11cf3f42009-10-27 22:16:29 +00003188FPToSIInst *FPToSIInst::clone_impl() const {
3189 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003190}
3191
Devang Patel11cf3f42009-10-27 22:16:29 +00003192PtrToIntInst *PtrToIntInst::clone_impl() const {
3193 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003194}
3195
Devang Patel11cf3f42009-10-27 22:16:29 +00003196IntToPtrInst *IntToPtrInst::clone_impl() const {
3197 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003198}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003199
Devang Patel11cf3f42009-10-27 22:16:29 +00003200BitCastInst *BitCastInst::clone_impl() const {
3201 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003202}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003203
Devang Patel11cf3f42009-10-27 22:16:29 +00003204CallInst *CallInst::clone_impl() const {
3205 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003206}
3207
Devang Patel11cf3f42009-10-27 22:16:29 +00003208SelectInst *SelectInst::clone_impl() const {
3209 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003210}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003211
Devang Patel11cf3f42009-10-27 22:16:29 +00003212VAArgInst *VAArgInst::clone_impl() const {
3213 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003214}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003215
Devang Patel11cf3f42009-10-27 22:16:29 +00003216ExtractElementInst *ExtractElementInst::clone_impl() const {
3217 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003218}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003219
Devang Patel11cf3f42009-10-27 22:16:29 +00003220InsertElementInst *InsertElementInst::clone_impl() const {
3221 return InsertElementInst::Create(getOperand(0),
3222 getOperand(1),
3223 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003224}
3225
Devang Patel11cf3f42009-10-27 22:16:29 +00003226ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3227 return new ShuffleVectorInst(getOperand(0),
3228 getOperand(1),
3229 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003230}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003231
Devang Patel11cf3f42009-10-27 22:16:29 +00003232PHINode *PHINode::clone_impl() const {
3233 return new PHINode(*this);
3234}
3235
3236ReturnInst *ReturnInst::clone_impl() const {
3237 return new(getNumOperands()) ReturnInst(*this);
3238}
3239
3240BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003241 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003242}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003243
Devang Patel11cf3f42009-10-27 22:16:29 +00003244SwitchInst *SwitchInst::clone_impl() const {
3245 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003246}
3247
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003248IndirectBrInst *IndirectBrInst::clone_impl() const {
3249 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003250}
3251
3252
Devang Patel11cf3f42009-10-27 22:16:29 +00003253InvokeInst *InvokeInst::clone_impl() const {
3254 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003255}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003256
Devang Patel11cf3f42009-10-27 22:16:29 +00003257UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003258 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003259 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003260}
3261
Devang Patel11cf3f42009-10-27 22:16:29 +00003262UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003263 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003264 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003265}