blob: 6fa904e4e44cb263c31c96f470f02207cd33965d [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
Bill Wendlingfae14752011-08-12 20:24:12 +0000169//===----------------------------------------------------------------------===//
170// LandingPadInst Implementation
171//===----------------------------------------------------------------------===//
172
173LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
174 unsigned NumReservedValues, const Twine &NameStr,
175 Instruction *InsertBefore)
176 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) {
177 init(PersonalityFn, 1 + NumReservedValues, NameStr);
178}
179
180LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
181 unsigned NumReservedValues, const Twine &NameStr,
182 BasicBlock *InsertAtEnd)
183 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) {
184 init(PersonalityFn, 1 + NumReservedValues, NameStr);
185}
186
187LandingPadInst::LandingPadInst(const LandingPadInst &LP)
188 : Instruction(LP.getType(), Instruction::LandingPad,
189 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
190 ReservedSpace(LP.getNumOperands()) {
191 Use *OL = OperandList, *InOL = LP.OperandList;
192 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
193 OL[I] = InOL[I];
194
195 setCleanup(LP.isCleanup());
196}
197
198LandingPadInst::~LandingPadInst() {
199 dropHungoffUses();
200}
201
202LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
203 unsigned NumReservedClauses,
204 const Twine &NameStr,
205 Instruction *InsertBefore) {
206 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
207 InsertBefore);
208}
209
210LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
211 unsigned NumReservedClauses,
212 const Twine &NameStr,
213 BasicBlock *InsertAtEnd) {
214 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
215 InsertAtEnd);
216}
217
218void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
219 const Twine &NameStr) {
220 ReservedSpace = NumReservedValues;
221 NumOperands = 1;
222 OperandList = allocHungoffUses(ReservedSpace);
223 OperandList[0] = PersFn;
224 setName(NameStr);
225 setCleanup(false);
226}
227
228/// growOperands - grow operands - This grows the operand list in response to a
229/// push_back style of operation. This grows the number of ops by 2 times.
230void LandingPadInst::growOperands(unsigned Size) {
231 unsigned e = getNumOperands();
232 if (ReservedSpace >= e + Size) return;
233 ReservedSpace = (e + Size / 2) * 2;
234
235 Use *NewOps = allocHungoffUses(ReservedSpace);
236 Use *OldOps = OperandList;
237 for (unsigned i = 0; i != e; ++i)
238 NewOps[i] = OldOps[i];
239
240 OperandList = NewOps;
241 Use::zap(OldOps, OldOps + e, true);
242}
243
244void LandingPadInst::addClause(Value *Val) {
245 unsigned OpNo = getNumOperands();
246 growOperands(1);
247 assert(OpNo < ReservedSpace && "Growing didn't work!");
248 ++NumOperands;
249 OperandList[OpNo] = Val;
250}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000251
252//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253// CallInst Implementation
254//===----------------------------------------------------------------------===//
255
Gordon Henriksen14a55692007-12-10 02:14:30 +0000256CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257}
258
Jay Foad5bd375a2011-07-15 08:37:34 +0000259void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
260 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000261 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262
Jay Foad5bd375a2011-07-15 08:37:34 +0000263#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000264 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
266
Jay Foad5bd375a2011-07-15 08:37:34 +0000267 assert((Args.size() == FTy->getNumParams() ||
268 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000269 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000270
271 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000272 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000273 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000274 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000275#endif
276
277 std::copy(Args.begin(), Args.end(), op_begin());
278 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279}
280
Jay Foad5bd375a2011-07-15 08:37:34 +0000281void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000283 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000284
Jay Foad5bd375a2011-07-15 08:37:34 +0000285#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000286 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
288
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000289 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000290#endif
291
292 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293}
294
Daniel Dunbar4975db62009-07-25 04:41:11 +0000295CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296 Instruction *InsertBefore)
297 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
298 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000299 Instruction::Call,
300 OperandTraits<CallInst>::op_end(this) - 1,
301 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000302 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303}
304
Daniel Dunbar4975db62009-07-25 04:41:11 +0000305CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306 BasicBlock *InsertAtEnd)
307 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
308 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000309 Instruction::Call,
310 OperandTraits<CallInst>::op_end(this) - 1,
311 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000312 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313}
314
Misha Brukmanb1c93172005-04-21 23:48:37 +0000315CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000316 : Instruction(CI.getType(), Instruction::Call,
317 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000318 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000319 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000320 setTailCall(CI.isTailCall());
321 setCallingConv(CI.getCallingConv());
322
Jay Foad5bd375a2011-07-15 08:37:34 +0000323 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000324 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325}
326
Devang Patel4c758ea2008-09-25 21:00:45 +0000327void CallInst::addAttribute(unsigned i, Attributes attr) {
328 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000329 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000330 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000331}
332
Devang Patel4c758ea2008-09-25 21:00:45 +0000333void CallInst::removeAttribute(unsigned i, Attributes attr) {
334 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000335 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000336 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000337}
338
Devang Patelba3fa6c2008-09-23 23:03:40 +0000339bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000340 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000341 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000342 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000343 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000344 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000345}
346
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000347/// IsConstantOne - Return true only if val is constant int 1
348static bool IsConstantOne(Value *val) {
349 assert(val && "IsConstantOne does not work with NULL val");
350 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
351}
352
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000353static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000354 BasicBlock *InsertAtEnd, Type *IntPtrTy,
355 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000356 Value *ArraySize, Function *MallocF,
357 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000358 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000359 "createMalloc needs either InsertBefore or InsertAtEnd");
360
361 // malloc(type) becomes:
362 // bitcast (i8* malloc(typeSize)) to type*
363 // malloc(type, arraySize) becomes:
364 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000365 if (!ArraySize)
366 ArraySize = ConstantInt::get(IntPtrTy, 1);
367 else if (ArraySize->getType() != IntPtrTy) {
368 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000369 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
370 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000371 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000372 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
373 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000374 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000375
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000376 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000377 if (IsConstantOne(AllocSize)) {
378 AllocSize = ArraySize; // Operand * 1 = Operand
379 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
380 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
381 false /*ZExt*/);
382 // Malloc arg is constant product of type size and array size
383 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
384 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000385 // Multiply type size by the array size...
386 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000387 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
388 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000389 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000390 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
391 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000392 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000393 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000394
Victor Hernandez788eaab2009-09-18 19:20:02 +0000395 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000396 // Create the call to Malloc.
397 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
398 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000399 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000400 Value *MallocFunc = MallocF;
401 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000402 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000403 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000404 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000405 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000406 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000407 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000408 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000409 Result = MCall;
410 if (Result->getType() != AllocPtrType)
411 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000412 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000413 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000414 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000415 Result = MCall;
416 if (Result->getType() != AllocPtrType) {
417 InsertAtEnd->getInstList().push_back(MCall);
418 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000419 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000420 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000421 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000422 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000423 if (Function *F = dyn_cast<Function>(MallocFunc)) {
424 MCall->setCallingConv(F->getCallingConv());
425 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
426 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000427 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000428
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000429 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000430}
431
432/// CreateMalloc - Generate the IR for a call to malloc:
433/// 1. Compute the malloc call's argument as the specified type's size,
434/// possibly multiplied by the array size if the array size is not
435/// constant 1.
436/// 2. Call malloc with that argument.
437/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000438Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000439 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000440 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000441 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000442 const Twine &Name) {
443 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000444 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445}
446
447/// CreateMalloc - Generate the IR for a call to malloc:
448/// 1. Compute the malloc call's argument as the specified type's size,
449/// possibly multiplied by the array size if the array size is not
450/// constant 1.
451/// 2. Call malloc with that argument.
452/// 3. Bitcast the result of the malloc call to the specified type.
453/// Note: This function does not add the bitcast to the basic block, that is the
454/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000455Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000456 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000457 Value *AllocSize, Value *ArraySize,
458 Function *MallocF, const Twine &Name) {
459 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000460 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000462
Victor Hernandeze2971492009-10-24 04:23:03 +0000463static Instruction* createFree(Value* Source, Instruction *InsertBefore,
464 BasicBlock *InsertAtEnd) {
465 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
466 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000467 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000468 "Can not free something of nonpointer type!");
469
470 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
471 Module* M = BB->getParent()->getParent();
472
Chris Lattner229907c2011-07-18 04:54:35 +0000473 Type *VoidTy = Type::getVoidTy(M->getContext());
474 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000475 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000476 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000477 CallInst* Result = NULL;
478 Value *PtrCast = Source;
479 if (InsertBefore) {
480 if (Source->getType() != IntPtrTy)
481 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
482 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
483 } else {
484 if (Source->getType() != IntPtrTy)
485 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
486 Result = CallInst::Create(FreeFunc, PtrCast, "");
487 }
488 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000489 if (Function *F = dyn_cast<Function>(FreeFunc))
490 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000491
492 return Result;
493}
494
495/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000496Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
497 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000498}
499
500/// CreateFree - Generate the IR for a call to the builtin free function.
501/// Note: This function does not add the call to the basic block, that is the
502/// responsibility of the caller.
503Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
504 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
505 assert(FreeCall && "CreateFree did not create a CallInst");
506 return FreeCall;
507}
508
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000509//===----------------------------------------------------------------------===//
510// InvokeInst Implementation
511//===----------------------------------------------------------------------===//
512
513void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000514 ArrayRef<Value *> Args, const Twine &NameStr) {
515 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000516 Op<-3>() = Fn;
517 Op<-2>() = IfNormal;
518 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000519
520#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000521 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000522 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000523
Jay Foad5bd375a2011-07-15 08:37:34 +0000524 assert(((Args.size() == FTy->getNumParams()) ||
525 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000526 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000527
Jay Foad5bd375a2011-07-15 08:37:34 +0000528 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000529 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000530 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000531 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000532#endif
533
534 std::copy(Args.begin(), Args.end(), op_begin());
535 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000536}
537
Misha Brukmanb1c93172005-04-21 23:48:37 +0000538InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000539 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000540 OperandTraits<InvokeInst>::op_end(this)
541 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000542 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000543 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000544 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000545 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000546 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000547}
548
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000549BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
550 return getSuccessor(idx);
551}
552unsigned InvokeInst::getNumSuccessorsV() const {
553 return getNumSuccessors();
554}
555void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
556 return setSuccessor(idx, B);
557}
558
Devang Patelba3fa6c2008-09-23 23:03:40 +0000559bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000560 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000561 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000562 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000563 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000564 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000565}
566
Devang Patel4c758ea2008-09-25 21:00:45 +0000567void InvokeInst::addAttribute(unsigned i, Attributes attr) {
568 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000569 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000570 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000571}
572
Devang Patel4c758ea2008-09-25 21:00:45 +0000573void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
574 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000575 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000576 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000577}
578
Bill Wendlingfae14752011-08-12 20:24:12 +0000579LandingPadInst *InvokeInst::getLandingPadInst() const {
580 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
581}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000582
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000583//===----------------------------------------------------------------------===//
584// ReturnInst Implementation
585//===----------------------------------------------------------------------===//
586
Chris Lattner2195fc42007-02-24 00:55:48 +0000587ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000588 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000589 OperandTraits<ReturnInst>::op_end(this) -
590 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000591 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000592 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000593 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000594 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000595}
596
Owen Anderson55f1c092009-08-13 21:58:54 +0000597ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
598 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000599 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
600 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000601 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000602 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000603}
Owen Anderson55f1c092009-08-13 21:58:54 +0000604ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
605 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000606 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
607 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000608 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000609 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000610}
Owen Anderson55f1c092009-08-13 21:58:54 +0000611ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
612 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000613 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000614}
615
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000616unsigned ReturnInst::getNumSuccessorsV() const {
617 return getNumSuccessors();
618}
619
Devang Patelae682fb2008-02-26 17:56:20 +0000620/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
621/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000622void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000623 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000624}
625
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000626BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000627 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628 return 0;
629}
630
Devang Patel59643e52008-02-23 00:35:18 +0000631ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000632}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000633
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000634//===----------------------------------------------------------------------===//
635// UnwindInst Implementation
636//===----------------------------------------------------------------------===//
637
Owen Anderson55f1c092009-08-13 21:58:54 +0000638UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
639 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
640 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000641}
Owen Anderson55f1c092009-08-13 21:58:54 +0000642UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
643 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
644 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000645}
646
647
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648unsigned UnwindInst::getNumSuccessorsV() const {
649 return getNumSuccessors();
650}
651
652void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000653 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000654}
655
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000657 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658 return 0;
659}
660
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000662// ResumeInst Implementation
663//===----------------------------------------------------------------------===//
664
665ResumeInst::ResumeInst(const ResumeInst &RI)
666 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
667 OperandTraits<ResumeInst>::op_begin(this), 1) {
668 Op<0>() = RI.Op<0>();
669}
670
671ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
672 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
673 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
674 Op<0>() = Exn;
675}
676
677ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
678 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
679 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
680 Op<0>() = Exn;
681}
682
683unsigned ResumeInst::getNumSuccessorsV() const {
684 return getNumSuccessors();
685}
686
687void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
688 llvm_unreachable("ResumeInst has no successors!");
689}
690
691BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
692 llvm_unreachable("ResumeInst has no successors!");
693 return 0;
694}
695
696//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000697// UnreachableInst Implementation
698//===----------------------------------------------------------------------===//
699
Owen Anderson55f1c092009-08-13 21:58:54 +0000700UnreachableInst::UnreachableInst(LLVMContext &Context,
701 Instruction *InsertBefore)
702 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
703 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
Owen Anderson55f1c092009-08-13 21:58:54 +0000705UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
706 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
707 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000708}
709
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000710unsigned UnreachableInst::getNumSuccessorsV() const {
711 return getNumSuccessors();
712}
713
714void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendlingad088e62011-07-30 05:42:50 +0000715 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000716}
717
718BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendlingad088e62011-07-30 05:42:50 +0000719 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000721}
722
723//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724// BranchInst Implementation
725//===----------------------------------------------------------------------===//
726
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727void BranchInst::AssertOK() {
728 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000729 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731}
732
Chris Lattner2195fc42007-02-24 00:55:48 +0000733BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000734 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000735 OperandTraits<BranchInst>::op_end(this) - 1,
736 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000737 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000738 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000739}
740BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
741 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000742 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000743 OperandTraits<BranchInst>::op_end(this) - 3,
744 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000745 Op<-1>() = IfTrue;
746 Op<-2>() = IfFalse;
747 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000748#ifndef NDEBUG
749 AssertOK();
750#endif
751}
752
753BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000754 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000755 OperandTraits<BranchInst>::op_end(this) - 1,
756 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000757 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000758 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000759}
760
761BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
762 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000763 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000764 OperandTraits<BranchInst>::op_end(this) - 3,
765 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000766 Op<-1>() = IfTrue;
767 Op<-2>() = IfFalse;
768 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000769#ifndef NDEBUG
770 AssertOK();
771#endif
772}
773
774
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000775BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000776 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000777 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
778 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000779 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000780 if (BI.getNumOperands() != 1) {
781 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000782 Op<-3>() = BI.Op<-3>();
783 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000785 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000786}
787
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000788void BranchInst::swapSuccessors() {
789 assert(isConditional() &&
790 "Cannot swap successors of an unconditional branch");
791 Op<-1>().swap(Op<-2>());
792
793 // Update profile metadata if present and it matches our structural
794 // expectations.
795 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
796 if (!ProfileData || ProfileData->getNumOperands() != 3)
797 return;
798
799 // The first operand is the name. Fetch them backwards and build a new one.
800 Value *Ops[] = {
801 ProfileData->getOperand(0),
802 ProfileData->getOperand(2),
803 ProfileData->getOperand(1)
804 };
805 setMetadata(LLVMContext::MD_prof,
806 MDNode::get(ProfileData->getContext(), Ops));
807}
808
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000809BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
810 return getSuccessor(idx);
811}
812unsigned BranchInst::getNumSuccessorsV() const {
813 return getNumSuccessors();
814}
815void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
816 setSuccessor(idx, B);
817}
818
819
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000821// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000822//===----------------------------------------------------------------------===//
823
Owen Andersonb6b25302009-07-14 23:09:55 +0000824static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000825 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000826 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000827 else {
828 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000829 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000830 assert(Amt->getType()->isIntegerTy() &&
831 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000832 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000833 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000834}
835
Chris Lattner229907c2011-07-18 04:54:35 +0000836AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000837 const Twine &Name, Instruction *InsertBefore)
838 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
839 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
840 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000841 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000842 setName(Name);
843}
844
Chris Lattner229907c2011-07-18 04:54:35 +0000845AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000846 const Twine &Name, BasicBlock *InsertAtEnd)
847 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
848 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
849 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000850 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000851 setName(Name);
852}
853
Chris Lattner229907c2011-07-18 04:54:35 +0000854AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000855 Instruction *InsertBefore)
856 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
857 getAISize(Ty->getContext(), 0), InsertBefore) {
858 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000859 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000860 setName(Name);
861}
862
Chris Lattner229907c2011-07-18 04:54:35 +0000863AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000864 BasicBlock *InsertAtEnd)
865 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
866 getAISize(Ty->getContext(), 0), InsertAtEnd) {
867 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000868 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000869 setName(Name);
870}
871
Chris Lattner229907c2011-07-18 04:54:35 +0000872AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000873 const Twine &Name, Instruction *InsertBefore)
874 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000875 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000876 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000877 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000878 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879}
880
Chris Lattner229907c2011-07-18 04:54:35 +0000881AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000882 const Twine &Name, BasicBlock *InsertAtEnd)
883 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000884 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000885 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000886 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000887 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000888}
889
Gordon Henriksen14a55692007-12-10 02:14:30 +0000890// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000891AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000892}
893
Victor Hernandez8acf2952009-10-23 21:09:37 +0000894void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000895 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000896 assert(Align <= MaximumAlignment &&
897 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000898 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000899 assert(getAlignment() == Align && "Alignment representation error!");
900}
901
Victor Hernandez8acf2952009-10-23 21:09:37 +0000902bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000903 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000904 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000905 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000906}
907
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000908Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000909 return getType()->getElementType();
910}
911
Chris Lattner8b291e62008-11-26 02:54:17 +0000912/// isStaticAlloca - Return true if this alloca is in the entry block of the
913/// function and is a constant size. If so, the code generator will fold it
914/// into the prolog/epilog code, so it is basically free.
915bool AllocaInst::isStaticAlloca() const {
916 // Must be constant size.
917 if (!isa<ConstantInt>(getArraySize())) return false;
918
919 // Must be in the entry block.
920 const BasicBlock *Parent = getParent();
921 return Parent == &Parent->getParent()->front();
922}
923
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000925// LoadInst Implementation
926//===----------------------------------------------------------------------===//
927
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000928void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000929 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000930 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000931 assert(!(isAtomic() && getAlignment() == 0) &&
932 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000933}
934
Daniel Dunbar4975db62009-07-25 04:41:11 +0000935LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000937 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000938 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000939 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000940 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000942 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000943}
944
Daniel Dunbar4975db62009-07-25 04:41:11 +0000945LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000946 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000947 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000948 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000949 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000950 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000951 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000952 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000953}
954
Daniel Dunbar4975db62009-07-25 04:41:11 +0000955LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000956 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000957 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000958 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000959 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000960 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000961 setAtomic(NotAtomic);
962 AssertOK();
963 setName(Name);
964}
965
966LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
967 BasicBlock *InsertAE)
968 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
969 Load, Ptr, InsertAE) {
970 setVolatile(isVolatile);
971 setAlignment(0);
972 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +0000973 AssertOK();
974 setName(Name);
975}
976
Daniel Dunbar4975db62009-07-25 04:41:11 +0000977LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000978 unsigned Align, Instruction *InsertBef)
979 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
980 Load, Ptr, InsertBef) {
981 setVolatile(isVolatile);
982 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000983 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000984 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000985 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000986}
987
Daniel Dunbar4975db62009-07-25 04:41:11 +0000988LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000989 unsigned Align, BasicBlock *InsertAE)
990 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
991 Load, Ptr, InsertAE) {
992 setVolatile(isVolatile);
993 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000994 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +0000995 AssertOK();
996 setName(Name);
997}
998
Eli Friedman59b66882011-08-09 23:02:53 +0000999LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1000 unsigned Align, AtomicOrdering Order,
1001 SynchronizationScope SynchScope,
1002 Instruction *InsertBef)
1003 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1004 Load, Ptr, InsertBef) {
1005 setVolatile(isVolatile);
1006 setAlignment(Align);
1007 setAtomic(Order, SynchScope);
1008 AssertOK();
1009 setName(Name);
1010}
1011
1012LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1013 unsigned Align, AtomicOrdering Order,
1014 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001015 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001016 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001017 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001018 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001019 setAlignment(Align);
1020 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001021 AssertOK();
1022 setName(Name);
1023}
1024
Daniel Dunbar27096822009-08-11 18:11:15 +00001025LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1026 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1027 Load, Ptr, InsertBef) {
1028 setVolatile(false);
1029 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001030 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001031 AssertOK();
1032 if (Name && Name[0]) setName(Name);
1033}
1034
1035LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1036 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1037 Load, Ptr, InsertAE) {
1038 setVolatile(false);
1039 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001040 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001041 AssertOK();
1042 if (Name && Name[0]) setName(Name);
1043}
1044
1045LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1046 Instruction *InsertBef)
1047: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1048 Load, Ptr, InsertBef) {
1049 setVolatile(isVolatile);
1050 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001051 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001052 AssertOK();
1053 if (Name && Name[0]) setName(Name);
1054}
1055
1056LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1057 BasicBlock *InsertAE)
1058 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1059 Load, Ptr, InsertAE) {
1060 setVolatile(isVolatile);
1061 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001062 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001063 AssertOK();
1064 if (Name && Name[0]) setName(Name);
1065}
1066
Christopher Lamb84485702007-04-22 19:24:39 +00001067void LoadInst::setAlignment(unsigned Align) {
1068 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001069 assert(Align <= MaximumAlignment &&
1070 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001071 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001072 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001073 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001074}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001075
1076//===----------------------------------------------------------------------===//
1077// StoreInst Implementation
1078//===----------------------------------------------------------------------===//
1079
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001080void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001081 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001082 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001083 "Ptr must have pointer type!");
1084 assert(getOperand(0)->getType() ==
1085 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001086 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001087 assert(!(isAtomic() && getAlignment() == 0) &&
1088 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001089}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001090
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001091
1092StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001093 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001094 OperandTraits<StoreInst>::op_begin(this),
1095 OperandTraits<StoreInst>::operands(this),
1096 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001097 Op<0>() = val;
1098 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001099 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001100 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001101 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001102 AssertOK();
1103}
1104
1105StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001106 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001107 OperandTraits<StoreInst>::op_begin(this),
1108 OperandTraits<StoreInst>::operands(this),
1109 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001110 Op<0>() = val;
1111 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001112 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001113 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001114 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001115 AssertOK();
1116}
1117
Misha Brukmanb1c93172005-04-21 23:48:37 +00001118StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001119 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001120 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001121 OperandTraits<StoreInst>::op_begin(this),
1122 OperandTraits<StoreInst>::operands(this),
1123 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001124 Op<0>() = val;
1125 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001126 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001127 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001128 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001129 AssertOK();
1130}
1131
1132StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1133 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001134 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001135 OperandTraits<StoreInst>::op_begin(this),
1136 OperandTraits<StoreInst>::operands(this),
1137 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001138 Op<0>() = val;
1139 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001140 setVolatile(isVolatile);
1141 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001142 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001143 AssertOK();
1144}
1145
Misha Brukmanb1c93172005-04-21 23:48:37 +00001146StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001147 unsigned Align, AtomicOrdering Order,
1148 SynchronizationScope SynchScope,
1149 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001150 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001151 OperandTraits<StoreInst>::op_begin(this),
1152 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001153 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001154 Op<0>() = val;
1155 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001156 setVolatile(isVolatile);
1157 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001158 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001159 AssertOK();
1160}
1161
1162StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001163 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001164 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001165 OperandTraits<StoreInst>::op_begin(this),
1166 OperandTraits<StoreInst>::operands(this),
1167 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001168 Op<0>() = val;
1169 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001170 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001171 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001172 setAtomic(NotAtomic);
1173 AssertOK();
1174}
1175
1176StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1177 unsigned Align, BasicBlock *InsertAtEnd)
1178 : Instruction(Type::getVoidTy(val->getContext()), Store,
1179 OperandTraits<StoreInst>::op_begin(this),
1180 OperandTraits<StoreInst>::operands(this),
1181 InsertAtEnd) {
1182 Op<0>() = val;
1183 Op<1>() = addr;
1184 setVolatile(isVolatile);
1185 setAlignment(Align);
1186 setAtomic(NotAtomic);
1187 AssertOK();
1188}
1189
1190StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1191 unsigned Align, AtomicOrdering Order,
1192 SynchronizationScope SynchScope,
1193 BasicBlock *InsertAtEnd)
1194 : Instruction(Type::getVoidTy(val->getContext()), Store,
1195 OperandTraits<StoreInst>::op_begin(this),
1196 OperandTraits<StoreInst>::operands(this),
1197 InsertAtEnd) {
1198 Op<0>() = val;
1199 Op<1>() = addr;
1200 setVolatile(isVolatile);
1201 setAlignment(Align);
1202 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001203 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001204}
1205
Christopher Lamb84485702007-04-22 19:24:39 +00001206void StoreInst::setAlignment(unsigned Align) {
1207 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001208 assert(Align <= MaximumAlignment &&
1209 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001210 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001211 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001212 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001213}
1214
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001215//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001216// AtomicCmpXchgInst Implementation
1217//===----------------------------------------------------------------------===//
1218
1219void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1220 AtomicOrdering Ordering,
1221 SynchronizationScope SynchScope) {
1222 Op<0>() = Ptr;
1223 Op<1>() = Cmp;
1224 Op<2>() = NewVal;
1225 setOrdering(Ordering);
1226 setSynchScope(SynchScope);
1227
1228 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1229 "All operands must be non-null!");
1230 assert(getOperand(0)->getType()->isPointerTy() &&
1231 "Ptr must have pointer type!");
1232 assert(getOperand(1)->getType() ==
1233 cast<PointerType>(getOperand(0)->getType())->getElementType()
1234 && "Ptr must be a pointer to Cmp type!");
1235 assert(getOperand(2)->getType() ==
1236 cast<PointerType>(getOperand(0)->getType())->getElementType()
1237 && "Ptr must be a pointer to NewVal type!");
1238 assert(Ordering != NotAtomic &&
1239 "AtomicCmpXchg instructions must be atomic!");
1240}
1241
1242AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1243 AtomicOrdering Ordering,
1244 SynchronizationScope SynchScope,
1245 Instruction *InsertBefore)
1246 : Instruction(Cmp->getType(), AtomicCmpXchg,
1247 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1248 OperandTraits<AtomicCmpXchgInst>::operands(this),
1249 InsertBefore) {
1250 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1251}
1252
1253AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1254 AtomicOrdering Ordering,
1255 SynchronizationScope SynchScope,
1256 BasicBlock *InsertAtEnd)
1257 : Instruction(Cmp->getType(), AtomicCmpXchg,
1258 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1259 OperandTraits<AtomicCmpXchgInst>::operands(this),
1260 InsertAtEnd) {
1261 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1262}
1263
1264//===----------------------------------------------------------------------===//
1265// AtomicRMWInst Implementation
1266//===----------------------------------------------------------------------===//
1267
1268void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1269 AtomicOrdering Ordering,
1270 SynchronizationScope SynchScope) {
1271 Op<0>() = Ptr;
1272 Op<1>() = Val;
1273 setOperation(Operation);
1274 setOrdering(Ordering);
1275 setSynchScope(SynchScope);
1276
1277 assert(getOperand(0) && getOperand(1) &&
1278 "All operands must be non-null!");
1279 assert(getOperand(0)->getType()->isPointerTy() &&
1280 "Ptr must have pointer type!");
1281 assert(getOperand(1)->getType() ==
1282 cast<PointerType>(getOperand(0)->getType())->getElementType()
1283 && "Ptr must be a pointer to Val type!");
1284 assert(Ordering != NotAtomic &&
1285 "AtomicRMW instructions must be atomic!");
1286}
1287
1288AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1289 AtomicOrdering Ordering,
1290 SynchronizationScope SynchScope,
1291 Instruction *InsertBefore)
1292 : Instruction(Val->getType(), AtomicRMW,
1293 OperandTraits<AtomicRMWInst>::op_begin(this),
1294 OperandTraits<AtomicRMWInst>::operands(this),
1295 InsertBefore) {
1296 Init(Operation, Ptr, Val, Ordering, SynchScope);
1297}
1298
1299AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1300 AtomicOrdering Ordering,
1301 SynchronizationScope SynchScope,
1302 BasicBlock *InsertAtEnd)
1303 : Instruction(Val->getType(), AtomicRMW,
1304 OperandTraits<AtomicRMWInst>::op_begin(this),
1305 OperandTraits<AtomicRMWInst>::operands(this),
1306 InsertAtEnd) {
1307 Init(Operation, Ptr, Val, Ordering, SynchScope);
1308}
1309
1310//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001311// FenceInst Implementation
1312//===----------------------------------------------------------------------===//
1313
1314FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1315 SynchronizationScope SynchScope,
1316 Instruction *InsertBefore)
1317 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1318 setOrdering(Ordering);
1319 setSynchScope(SynchScope);
1320}
1321
1322FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1323 SynchronizationScope SynchScope,
1324 BasicBlock *InsertAtEnd)
1325 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1326 setOrdering(Ordering);
1327 setSynchScope(SynchScope);
1328}
1329
1330//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001331// GetElementPtrInst Implementation
1332//===----------------------------------------------------------------------===//
1333
Jay Foadd1b78492011-07-25 09:48:08 +00001334void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001335 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001336 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1337 OperandList[0] = Ptr;
1338 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001339 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001340}
1341
Gabor Greiff6caff662008-05-10 08:32:32 +00001342GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001343 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001344 OperandTraits<GetElementPtrInst>::op_end(this)
1345 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001346 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001347 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001348 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001349}
1350
Chris Lattner6090a422009-03-09 04:46:40 +00001351/// getIndexedType - Returns the type of the element that would be accessed with
1352/// a gep instruction with the specified parameters.
1353///
1354/// The Idxs pointer should point to a continuous piece of memory containing the
1355/// indices, either as Value* or uint64_t.
1356///
1357/// A null type is returned if the indices are invalid for the specified
1358/// pointer type.
1359///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001360template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001361static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Chris Lattner229907c2011-07-18 04:54:35 +00001362 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001363 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001364 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001365
Chris Lattner6090a422009-03-09 04:46:40 +00001366 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001367 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001368 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001369
1370 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001371 // it cannot be 'stepped over'.
1372 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001373 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001374
Dan Gohman1ecaf452008-05-31 00:58:22 +00001375 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001376 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001377 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001378 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001379 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001380 if (!CT->indexValid(Index)) return 0;
1381 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001382 }
Jay Foadd1b78492011-07-25 09:48:08 +00001383 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001384}
1385
Jay Foadd1b78492011-07-25 09:48:08 +00001386Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1387 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001388}
1389
Chris Lattner229907c2011-07-18 04:54:35 +00001390Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001391 ArrayRef<Constant *> IdxList) {
1392 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001393}
1394
Jay Foadd1b78492011-07-25 09:48:08 +00001395Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1396 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001397}
1398
Chris Lattner45f15572007-04-14 00:12:57 +00001399/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1400/// zeros. If so, the result pointer and the first operand have the same
1401/// value, just potentially different types.
1402bool GetElementPtrInst::hasAllZeroIndices() const {
1403 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1404 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1405 if (!CI->isZero()) return false;
1406 } else {
1407 return false;
1408 }
1409 }
1410 return true;
1411}
1412
Chris Lattner27058292007-04-27 20:35:56 +00001413/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1414/// constant integers. If so, the result pointer and the first operand have
1415/// a constant offset between them.
1416bool GetElementPtrInst::hasAllConstantIndices() const {
1417 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1418 if (!isa<ConstantInt>(getOperand(i)))
1419 return false;
1420 }
1421 return true;
1422}
1423
Dan Gohman1b849082009-09-07 23:54:19 +00001424void GetElementPtrInst::setIsInBounds(bool B) {
1425 cast<GEPOperator>(this)->setIsInBounds(B);
1426}
Chris Lattner45f15572007-04-14 00:12:57 +00001427
Nick Lewycky28a5f252009-09-27 21:33:04 +00001428bool GetElementPtrInst::isInBounds() const {
1429 return cast<GEPOperator>(this)->isInBounds();
1430}
1431
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001432//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001433// ExtractElementInst Implementation
1434//===----------------------------------------------------------------------===//
1435
1436ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001437 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001438 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001439 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001440 ExtractElement,
1441 OperandTraits<ExtractElementInst>::op_begin(this),
1442 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001443 assert(isValidOperands(Val, Index) &&
1444 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001445 Op<0>() = Val;
1446 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001447 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001448}
1449
1450ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001451 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001452 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001453 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001454 ExtractElement,
1455 OperandTraits<ExtractElementInst>::op_begin(this),
1456 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001457 assert(isValidOperands(Val, Index) &&
1458 "Invalid extractelement instruction operands!");
1459
Gabor Greif2d3024d2008-05-26 21:33:52 +00001460 Op<0>() = Val;
1461 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001462 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001463}
1464
Chris Lattner65511ff2006-10-05 06:24:58 +00001465
Chris Lattner54865b32006-04-08 04:05:48 +00001466bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001467 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001468 return false;
1469 return true;
1470}
1471
1472
Robert Bocchino23004482006-01-10 19:05:34 +00001473//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001474// InsertElementInst Implementation
1475//===----------------------------------------------------------------------===//
1476
Chris Lattner54865b32006-04-08 04:05:48 +00001477InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001478 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001479 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001480 : Instruction(Vec->getType(), InsertElement,
1481 OperandTraits<InsertElementInst>::op_begin(this),
1482 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001483 assert(isValidOperands(Vec, Elt, Index) &&
1484 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001485 Op<0>() = Vec;
1486 Op<1>() = Elt;
1487 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001488 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001489}
1490
Chris Lattner54865b32006-04-08 04:05:48 +00001491InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001492 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001493 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001494 : Instruction(Vec->getType(), InsertElement,
1495 OperandTraits<InsertElementInst>::op_begin(this),
1496 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001497 assert(isValidOperands(Vec, Elt, Index) &&
1498 "Invalid insertelement instruction operands!");
1499
Gabor Greif2d3024d2008-05-26 21:33:52 +00001500 Op<0>() = Vec;
1501 Op<1>() = Elt;
1502 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001503 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001504}
1505
Chris Lattner54865b32006-04-08 04:05:48 +00001506bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1507 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001508 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001509 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001510
Reid Spencerd84d35b2007-02-15 02:26:10 +00001511 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001512 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001513
Duncan Sands9dff9be2010-02-15 16:12:20 +00001514 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001515 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001516 return true;
1517}
1518
1519
Robert Bocchinoca27f032006-01-17 20:07:22 +00001520//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001521// ShuffleVectorInst Implementation
1522//===----------------------------------------------------------------------===//
1523
1524ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001525 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001526 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001527: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001528 cast<VectorType>(Mask->getType())->getNumElements()),
1529 ShuffleVector,
1530 OperandTraits<ShuffleVectorInst>::op_begin(this),
1531 OperandTraits<ShuffleVectorInst>::operands(this),
1532 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001533 assert(isValidOperands(V1, V2, Mask) &&
1534 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001535 Op<0>() = V1;
1536 Op<1>() = V2;
1537 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001538 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001539}
1540
1541ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001542 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001543 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001544: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1545 cast<VectorType>(Mask->getType())->getNumElements()),
1546 ShuffleVector,
1547 OperandTraits<ShuffleVectorInst>::op_begin(this),
1548 OperandTraits<ShuffleVectorInst>::operands(this),
1549 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001550 assert(isValidOperands(V1, V2, Mask) &&
1551 "Invalid shuffle vector instruction operands!");
1552
Gabor Greif2d3024d2008-05-26 21:33:52 +00001553 Op<0>() = V1;
1554 Op<1>() = V2;
1555 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001556 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001557}
1558
Mon P Wang25f01062008-11-10 04:46:22 +00001559bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001560 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001561 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001562 return false;
1563
Chris Lattner229907c2011-07-18 04:54:35 +00001564 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001565 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001566 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001567
1568 // Check to see if Mask is valid.
1569 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001570 VectorType *VTy = cast<VectorType>(V1->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001571 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1572 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1573 if (CI->uge(VTy->getNumElements()*2))
1574 return false;
1575 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1576 return false;
1577 }
1578 }
Mon P Wang6ebf4012011-10-26 00:34:48 +00001579 } else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask)) {
1580 // The bitcode reader can create a place holder for a forward reference
1581 // used as the shuffle mask. When this occurs, the shuffle mask will
1582 // fall into this case and fail. To avoid this error, do this bit of
1583 // ugliness to allow such a mask pass.
1584 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(Mask)) {
1585 if (CE->getOpcode() == Instruction::UserOp1)
1586 return true;
1587 }
Nate Begeman60a31c32010-08-13 00:16:46 +00001588 return false;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001589 }
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001590 return true;
1591}
1592
Chris Lattnerf724e342008-03-02 05:28:33 +00001593/// getMaskValue - Return the index from the shuffle mask for the specified
1594/// output result. This is either -1 if the element is undef or a number less
1595/// than 2*numelements.
1596int ShuffleVectorInst::getMaskValue(unsigned i) const {
1597 const Constant *Mask = cast<Constant>(getOperand(2));
1598 if (isa<UndefValue>(Mask)) return -1;
1599 if (isa<ConstantAggregateZero>(Mask)) return 0;
1600 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1601 assert(i < MaskCV->getNumOperands() && "Index out of range");
1602
1603 if (isa<UndefValue>(MaskCV->getOperand(i)))
1604 return -1;
1605 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1606}
1607
Dan Gohman12fce772008-05-15 19:50:34 +00001608//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001609// InsertValueInst Class
1610//===----------------------------------------------------------------------===//
1611
Jay Foad57aa6362011-07-13 10:26:04 +00001612void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1613 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001614 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001615
1616 // There's no fundamental reason why we require at least one index
1617 // (other than weirdness with &*IdxBegin being invalid; see
1618 // getelementptr's init routine for example). But there's no
1619 // present need to support it.
1620 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1621
1622 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001623 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001624 Op<0>() = Agg;
1625 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001626
Jay Foad57aa6362011-07-13 10:26:04 +00001627 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001628 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001629}
1630
1631InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001632 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001633 OperandTraits<InsertValueInst>::op_begin(this), 2),
1634 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001635 Op<0>() = IVI.getOperand(0);
1636 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001637 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001638}
1639
1640//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001641// ExtractValueInst Class
1642//===----------------------------------------------------------------------===//
1643
Jay Foad57aa6362011-07-13 10:26:04 +00001644void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001645 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001646
Jay Foad57aa6362011-07-13 10:26:04 +00001647 // There's no fundamental reason why we require at least one index.
1648 // But there's no present need to support it.
1649 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001650
Jay Foad57aa6362011-07-13 10:26:04 +00001651 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001652 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001653}
1654
1655ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001656 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001657 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001658 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001659}
1660
Dan Gohman12fce772008-05-15 19:50:34 +00001661// getIndexedType - Returns the type of the element that would be extracted
1662// with an extractvalue instruction with the specified parameters.
1663//
1664// A null type is returned if the indices are invalid for the specified
1665// pointer type.
1666//
Chris Lattner229907c2011-07-18 04:54:35 +00001667Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001668 ArrayRef<unsigned> Idxs) {
1669 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001670 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001671 // We can't use CompositeType::indexValid(Index) here.
1672 // indexValid() always returns true for arrays because getelementptr allows
1673 // out-of-bounds indices. Since we don't allow those for extractvalue and
1674 // insertvalue we need to check array indexing manually.
1675 // Since the only other types we can index into are struct types it's just
1676 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001677 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001678 if (Index >= AT->getNumElements())
1679 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001680 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001681 if (Index >= ST->getNumElements())
1682 return 0;
1683 } else {
1684 // Not a valid type to index into.
1685 return 0;
1686 }
1687
1688 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001689 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001690 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001691}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001692
1693//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001694// BinaryOperator Class
1695//===----------------------------------------------------------------------===//
1696
Chris Lattner2195fc42007-02-24 00:55:48 +00001697BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001698 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001699 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001700 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001701 OperandTraits<BinaryOperator>::op_begin(this),
1702 OperandTraits<BinaryOperator>::operands(this),
1703 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001704 Op<0>() = S1;
1705 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001706 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001707 setName(Name);
1708}
1709
1710BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001711 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001712 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001713 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001714 OperandTraits<BinaryOperator>::op_begin(this),
1715 OperandTraits<BinaryOperator>::operands(this),
1716 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001717 Op<0>() = S1;
1718 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001719 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001720 setName(Name);
1721}
1722
1723
1724void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001725 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001726 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001727 assert(LHS->getType() == RHS->getType() &&
1728 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001729#ifndef NDEBUG
1730 switch (iType) {
1731 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001732 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001733 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001734 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001735 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001736 "Tried to create an integer operation on a non-integer type!");
1737 break;
1738 case FAdd: case FSub:
1739 case FMul:
1740 assert(getType() == LHS->getType() &&
1741 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001742 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001743 "Tried to create a floating-point operation on a "
1744 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001745 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001746 case UDiv:
1747 case SDiv:
1748 assert(getType() == LHS->getType() &&
1749 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001750 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001751 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001752 "Incorrect operand type (not integer) for S/UDIV");
1753 break;
1754 case FDiv:
1755 assert(getType() == LHS->getType() &&
1756 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001757 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001758 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001759 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001760 case URem:
1761 case SRem:
1762 assert(getType() == LHS->getType() &&
1763 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001764 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001765 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001766 "Incorrect operand type (not integer) for S/UREM");
1767 break;
1768 case FRem:
1769 assert(getType() == LHS->getType() &&
1770 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001771 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001772 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001773 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001774 case Shl:
1775 case LShr:
1776 case AShr:
1777 assert(getType() == LHS->getType() &&
1778 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001779 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001780 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001781 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001782 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001783 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001784 case And: case Or:
1785 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001786 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001787 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001788 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001789 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001790 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001791 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001792 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001793 default:
1794 break;
1795 }
1796#endif
1797}
1798
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001799BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001800 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001801 Instruction *InsertBefore) {
1802 assert(S1->getType() == S2->getType() &&
1803 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001804 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001805}
1806
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001807BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001808 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001809 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001810 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001811 InsertAtEnd->getInstList().push_back(Res);
1812 return Res;
1813}
1814
Dan Gohman5476cfd2009-08-12 16:23:25 +00001815BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001816 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001817 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001818 return new BinaryOperator(Instruction::Sub,
1819 zero, Op,
1820 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001821}
1822
Dan Gohman5476cfd2009-08-12 16:23:25 +00001823BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001824 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001825 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001826 return new BinaryOperator(Instruction::Sub,
1827 zero, Op,
1828 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001829}
1830
Dan Gohman4ab44202009-12-18 02:58:50 +00001831BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1832 Instruction *InsertBefore) {
1833 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1834 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1835}
1836
1837BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1838 BasicBlock *InsertAtEnd) {
1839 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1840 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1841}
1842
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001843BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1844 Instruction *InsertBefore) {
1845 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1846 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1847}
1848
1849BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1850 BasicBlock *InsertAtEnd) {
1851 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1852 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1853}
1854
Dan Gohman5476cfd2009-08-12 16:23:25 +00001855BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001856 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001857 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001858 return new BinaryOperator(Instruction::FSub,
1859 zero, Op,
1860 Op->getType(), Name, InsertBefore);
1861}
1862
Dan Gohman5476cfd2009-08-12 16:23:25 +00001863BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001864 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001865 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001866 return new BinaryOperator(Instruction::FSub,
1867 zero, Op,
1868 Op->getType(), Name, InsertAtEnd);
1869}
1870
Dan Gohman5476cfd2009-08-12 16:23:25 +00001871BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001872 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001873 Constant *C;
Chris Lattner229907c2011-07-18 04:54:35 +00001874 if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001875 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001876 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001877 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001878 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001879 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001880 }
1881
1882 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001883 Op->getType(), Name, InsertBefore);
1884}
1885
Dan Gohman5476cfd2009-08-12 16:23:25 +00001886BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001887 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001888 Constant *AllOnes;
Chris Lattner229907c2011-07-18 04:54:35 +00001889 if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001890 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001891 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001892 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001893 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001894 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001895 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001896 }
1897
1898 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001899 Op->getType(), Name, InsertAtEnd);
1900}
1901
1902
1903// isConstantAllOnes - Helper function for several functions below
1904static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001905 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1906 return CI->isAllOnesValue();
1907 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1908 return CV->isAllOnesValue();
1909 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001910}
1911
Owen Andersonbb2501b2009-07-13 22:18:28 +00001912bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001913 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1914 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001915 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1916 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001917 return false;
1918}
1919
Owen Andersonbb2501b2009-07-13 22:18:28 +00001920bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001921 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1922 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001923 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001924 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001925 return false;
1926}
1927
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001928bool BinaryOperator::isNot(const Value *V) {
1929 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1930 return (Bop->getOpcode() == Instruction::Xor &&
1931 (isConstantAllOnes(Bop->getOperand(1)) ||
1932 isConstantAllOnes(Bop->getOperand(0))));
1933 return false;
1934}
1935
Chris Lattner2c7d1772005-04-24 07:28:37 +00001936Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001937 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001938}
1939
Chris Lattner2c7d1772005-04-24 07:28:37 +00001940const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1941 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001942}
1943
Dan Gohmana5b96452009-06-04 22:49:04 +00001944Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001945 return cast<BinaryOperator>(BinOp)->getOperand(1);
1946}
1947
1948const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1949 return getFNegArgument(const_cast<Value*>(BinOp));
1950}
1951
Chris Lattner2c7d1772005-04-24 07:28:37 +00001952Value *BinaryOperator::getNotArgument(Value *BinOp) {
1953 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1954 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1955 Value *Op0 = BO->getOperand(0);
1956 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001957 if (isConstantAllOnes(Op0)) return Op1;
1958
1959 assert(isConstantAllOnes(Op1));
1960 return Op0;
1961}
1962
Chris Lattner2c7d1772005-04-24 07:28:37 +00001963const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1964 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001965}
1966
1967
1968// swapOperands - Exchange the two operands to this instruction. This
1969// instruction is safe to use on any binary instruction and does not
1970// modify the semantics of the instruction. If the instruction is
1971// order dependent (SetLT f.e.) the opcode is changed.
1972//
1973bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001974 if (!isCommutative())
1975 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001976 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001977 return false;
1978}
1979
Dan Gohman1b849082009-09-07 23:54:19 +00001980void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1981 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1982}
1983
1984void BinaryOperator::setHasNoSignedWrap(bool b) {
1985 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1986}
1987
1988void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001989 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001990}
1991
Nick Lewycky28a5f252009-09-27 21:33:04 +00001992bool BinaryOperator::hasNoUnsignedWrap() const {
1993 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1994}
1995
1996bool BinaryOperator::hasNoSignedWrap() const {
1997 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1998}
1999
2000bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002001 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002002}
2003
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002004//===----------------------------------------------------------------------===//
2005// CastInst Class
2006//===----------------------------------------------------------------------===//
2007
David Blaikie3a15e142011-12-01 08:00:17 +00002008void CastInst::anchor() {}
2009
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010// Just determine if this cast only deals with integral->integral conversion.
2011bool CastInst::isIntegerCast() const {
2012 switch (getOpcode()) {
2013 default: return false;
2014 case Instruction::ZExt:
2015 case Instruction::SExt:
2016 case Instruction::Trunc:
2017 return true;
2018 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002019 return getOperand(0)->getType()->isIntegerTy() &&
2020 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002021 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002022}
2023
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002024bool CastInst::isLosslessCast() const {
2025 // Only BitCast can be lossless, exit fast if we're not BitCast
2026 if (getOpcode() != Instruction::BitCast)
2027 return false;
2028
2029 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002030 Type* SrcTy = getOperand(0)->getType();
2031 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002032 if (SrcTy == DstTy)
2033 return true;
2034
Reid Spencer8d9336d2006-12-31 05:26:44 +00002035 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002036 if (SrcTy->isPointerTy())
2037 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038 return false; // Other types have no identity values
2039}
2040
2041/// This function determines if the CastInst does not require any bits to be
2042/// changed in order to effect the cast. Essentially, it identifies cases where
2043/// no code gen is necessary for the cast, hence the name no-op cast. For
2044/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002045/// # bitcast i32* %x to i8*
2046/// # bitcast <2 x i32> %x to <4 x i16>
2047/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002048/// @brief Determine if the described cast is a no-op.
2049bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002050 Type *SrcTy,
2051 Type *DestTy,
2052 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002053 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002054 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002055 assert(0 && "Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056 case Instruction::Trunc:
2057 case Instruction::ZExt:
2058 case Instruction::SExt:
2059 case Instruction::FPTrunc:
2060 case Instruction::FPExt:
2061 case Instruction::UIToFP:
2062 case Instruction::SIToFP:
2063 case Instruction::FPToUI:
2064 case Instruction::FPToSI:
2065 return false; // These always modify bits
2066 case Instruction::BitCast:
2067 return true; // BitCast never modifies bits.
2068 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002069 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002070 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002071 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002072 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002073 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074 }
2075}
2076
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002077/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002078bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002079 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2080}
2081
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002082/// This function determines if a pair of casts can be eliminated and what
2083/// opcode should be used in the elimination. This assumes that there are two
2084/// instructions like this:
2085/// * %F = firstOpcode SrcTy %x to MidTy
2086/// * %S = secondOpcode MidTy %F to DstTy
2087/// The function returns a resultOpcode so these two casts can be replaced with:
2088/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2089/// If no such cast is permited, the function returns 0.
2090unsigned CastInst::isEliminableCastPair(
2091 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002092 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002093 // Define the 144 possibilities for these two cast instructions. The values
2094 // in this matrix determine what to do in a given situation and select the
2095 // case in the switch below. The rows correspond to firstOp, the columns
2096 // correspond to secondOp. In looking at the table below, keep in mind
2097 // the following cast properties:
2098 //
2099 // Size Compare Source Destination
2100 // Operator Src ? Size Type Sign Type Sign
2101 // -------- ------------ ------------------- ---------------------
2102 // TRUNC > Integer Any Integral Any
2103 // ZEXT < Integral Unsigned Integer Any
2104 // SEXT < Integral Signed Integer Any
2105 // FPTOUI n/a FloatPt n/a Integral Unsigned
2106 // FPTOSI n/a FloatPt n/a Integral Signed
2107 // UITOFP n/a Integral Unsigned FloatPt n/a
2108 // SITOFP n/a Integral Signed FloatPt n/a
2109 // FPTRUNC > FloatPt n/a FloatPt n/a
2110 // FPEXT < FloatPt n/a FloatPt n/a
2111 // PTRTOINT n/a Pointer n/a Integral Unsigned
2112 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002113 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002114 //
2115 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002116 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2117 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002118 // of the produced value (we no longer know the top-part is all zeros).
2119 // Further this conversion is often much more expensive for typical hardware,
2120 // and causes issues when building libgcc. We disallow fptosi+sext for the
2121 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002122 const unsigned numCastOps =
2123 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2124 static const uint8_t CastResults[numCastOps][numCastOps] = {
2125 // T F F U S F F P I B -+
2126 // R Z S P P I I T P 2 N T |
2127 // U E E 2 2 2 2 R E I T C +- secondOp
2128 // N X X U S F F N X N 2 V |
2129 // C T T I I P P C T T P T -+
2130 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2131 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2132 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002133 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2134 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002135 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2136 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2137 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2138 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2139 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2140 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2141 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2142 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002143
2144 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002145 // merging. However, bitcast of A->B->A are allowed.
2146 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2147 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2148 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2149
2150 // Check if any of the bitcasts convert scalars<->vectors.
2151 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2152 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2153 // Unless we are bitcasing to the original type, disallow optimizations.
2154 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002155
2156 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2157 [secondOp-Instruction::CastOpsBegin];
2158 switch (ElimCase) {
2159 case 0:
2160 // categorically disallowed
2161 return 0;
2162 case 1:
2163 // allowed, use first cast's opcode
2164 return firstOp;
2165 case 2:
2166 // allowed, use second cast's opcode
2167 return secondOp;
2168 case 3:
2169 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002170 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002171 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002172 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002173 return firstOp;
2174 return 0;
2175 case 4:
2176 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002177 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002178 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179 return firstOp;
2180 return 0;
2181 case 5:
2182 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002183 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002184 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002185 return secondOp;
2186 return 0;
2187 case 6:
2188 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002189 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002190 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002191 return secondOp;
2192 return 0;
2193 case 7: {
2194 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002195 if (!IntPtrTy)
2196 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002197 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2198 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002199 if (MidSize >= PtrSize)
2200 return Instruction::BitCast;
2201 return 0;
2202 }
2203 case 8: {
2204 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2205 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2206 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002207 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2208 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002209 if (SrcSize == DstSize)
2210 return Instruction::BitCast;
2211 else if (SrcSize < DstSize)
2212 return firstOp;
2213 return secondOp;
2214 }
2215 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2216 return Instruction::ZExt;
2217 case 10:
2218 // fpext followed by ftrunc is allowed if the bit size returned to is
2219 // the same as the original, in which case its just a bitcast
2220 if (SrcTy == DstTy)
2221 return Instruction::BitCast;
2222 return 0; // If the types are not the same we can't eliminate it.
2223 case 11:
2224 // bitcast followed by ptrtoint is allowed as long as the bitcast
2225 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002226 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002227 return secondOp;
2228 return 0;
2229 case 12:
2230 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002231 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232 return firstOp;
2233 return 0;
2234 case 13: {
2235 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002236 if (!IntPtrTy)
2237 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002238 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2239 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2240 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002241 if (SrcSize <= PtrSize && SrcSize == DstSize)
2242 return Instruction::BitCast;
2243 return 0;
2244 }
2245 case 99:
2246 // cast combination can't happen (error in input). This is for all cases
2247 // where the MidTy is not the same for the two cast instructions.
Richard Trieua318b8d2011-09-21 03:09:09 +00002248 assert(0 && "Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002249 return 0;
2250 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002251 assert(0 && "Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252 return 0;
2253 }
2254 return 0;
2255}
2256
Chris Lattner229907c2011-07-18 04:54:35 +00002257CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002258 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002259 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002260 // Construct and return the appropriate CastInst subclass
2261 switch (op) {
2262 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2263 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2264 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2265 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2266 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2267 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2268 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2269 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2270 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2271 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2272 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2273 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2274 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002275 assert(0 && "Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002276 }
2277 return 0;
2278}
2279
Chris Lattner229907c2011-07-18 04:54:35 +00002280CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002281 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002282 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283 // Construct and return the appropriate CastInst subclass
2284 switch (op) {
2285 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2286 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2287 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2288 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2289 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2290 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2291 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2292 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2293 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2294 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2295 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2296 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2297 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002298 assert(0 && "Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 }
2300 return 0;
2301}
2302
Chris Lattner229907c2011-07-18 04:54:35 +00002303CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002304 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002305 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002306 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002307 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2308 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002309}
2310
Chris Lattner229907c2011-07-18 04:54:35 +00002311CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002312 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002313 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002314 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002315 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2316 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002317}
2318
Chris Lattner229907c2011-07-18 04:54:35 +00002319CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002320 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002321 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002322 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002323 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2324 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002325}
2326
Chris Lattner229907c2011-07-18 04:54:35 +00002327CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002328 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002329 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002330 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002331 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2332 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002333}
2334
Chris Lattner229907c2011-07-18 04:54:35 +00002335CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002336 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002337 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002338 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002339 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2340 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002341}
2342
Chris Lattner229907c2011-07-18 04:54:35 +00002343CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002344 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002345 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002346 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002347 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2348 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002349}
2350
Chris Lattner229907c2011-07-18 04:54:35 +00002351CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002352 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002353 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002354 assert(S->getType()->isPointerTy() && "Invalid cast");
2355 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002356 "Invalid cast");
2357
Duncan Sands9dff9be2010-02-15 16:12:20 +00002358 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002359 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2360 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002361}
2362
2363/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002364CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002365 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002366 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002367 assert(S->getType()->isPointerTy() && "Invalid cast");
2368 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002369 "Invalid cast");
2370
Duncan Sands9dff9be2010-02-15 16:12:20 +00002371 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002372 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2373 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002374}
2375
Chris Lattner229907c2011-07-18 04:54:35 +00002376CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002377 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002378 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002379 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002380 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002381 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2382 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002383 Instruction::CastOps opcode =
2384 (SrcBits == DstBits ? Instruction::BitCast :
2385 (SrcBits > DstBits ? Instruction::Trunc :
2386 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002387 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002388}
2389
Chris Lattner229907c2011-07-18 04:54:35 +00002390CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002391 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002392 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002393 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002394 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002395 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2396 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002397 Instruction::CastOps opcode =
2398 (SrcBits == DstBits ? Instruction::BitCast :
2399 (SrcBits > DstBits ? Instruction::Trunc :
2400 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002401 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002402}
2403
Chris Lattner229907c2011-07-18 04:54:35 +00002404CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002405 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002406 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002407 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002408 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002409 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2410 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002411 Instruction::CastOps opcode =
2412 (SrcBits == DstBits ? Instruction::BitCast :
2413 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002414 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002415}
2416
Chris Lattner229907c2011-07-18 04:54:35 +00002417CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002418 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002419 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002420 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002421 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002422 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2423 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002424 Instruction::CastOps opcode =
2425 (SrcBits == DstBits ? Instruction::BitCast :
2426 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002427 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002428}
2429
Duncan Sands55e50902008-01-06 10:12:28 +00002430// Check whether it is valid to call getCastOpcode for these types.
2431// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002432bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002433 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2434 return false;
2435
2436 if (SrcTy == DestTy)
2437 return true;
2438
Chris Lattner229907c2011-07-18 04:54:35 +00002439 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2440 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002441 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2442 // An element by element cast. Valid if casting the elements is valid.
2443 SrcTy = SrcVecTy->getElementType();
2444 DestTy = DestVecTy->getElementType();
2445 }
2446
Duncan Sands55e50902008-01-06 10:12:28 +00002447 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002448 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2449 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002450
2451 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002452 if (DestTy->isIntegerTy()) { // Casting to integral
2453 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002454 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002455 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002456 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002457 } else if (SrcTy->isVectorTy()) { // Casting from vector
2458 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002459 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002460 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002461 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002462 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2463 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002464 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002465 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002466 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002467 } else if (SrcTy->isVectorTy()) { // Casting from vector
2468 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002469 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002470 return false;
2471 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002472 } else if (DestTy->isVectorTy()) { // Casting to vector
2473 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002474 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002475 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002476 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002477 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002478 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002479 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002480 return false;
2481 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002482 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002483 if (SrcTy->isVectorTy()) {
2484 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002485 } else {
2486 return false;
2487 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002488 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002489 return false;
2490 }
2491}
2492
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493// Provide a way to get a "cast" where the cast opcode is inferred from the
2494// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002495// logic in the castIsValid function below. This axiom should hold:
2496// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2497// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002499// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002501CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002502 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2503 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504
Duncan Sands55e50902008-01-06 10:12:28 +00002505 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2506 "Only first class types are castable!");
2507
Duncan Sandsa8514532011-05-18 07:13:41 +00002508 if (SrcTy == DestTy)
2509 return BitCast;
2510
Chris Lattner229907c2011-07-18 04:54:35 +00002511 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2512 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002513 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2514 // An element by element cast. Find the appropriate opcode based on the
2515 // element types.
2516 SrcTy = SrcVecTy->getElementType();
2517 DestTy = DestVecTy->getElementType();
2518 }
2519
2520 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002521 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2522 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002523
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002525 if (DestTy->isIntegerTy()) { // Casting to integral
2526 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527 if (DestBits < SrcBits)
2528 return Trunc; // int -> smaller int
2529 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002530 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531 return SExt; // signed -> SEXT
2532 else
2533 return ZExt; // unsigned -> ZEXT
2534 } else {
2535 return BitCast; // Same size, No-op cast
2536 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002537 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002538 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539 return FPToSI; // FP -> sint
2540 else
2541 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002542 } else if (SrcTy->isVectorTy()) {
2543 assert(DestBits == SrcBits &&
2544 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545 return BitCast; // Same size, no-op cast
2546 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002547 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548 "Casting from a value that is not first-class type");
2549 return PtrToInt; // ptr -> int
2550 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002551 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2552 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002553 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002554 return SIToFP; // sint -> FP
2555 else
2556 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002557 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558 if (DestBits < SrcBits) {
2559 return FPTrunc; // FP -> smaller FP
2560 } else if (DestBits > SrcBits) {
2561 return FPExt; // FP -> larger FP
2562 } else {
2563 return BitCast; // same size, no-op cast
2564 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002565 } else if (SrcTy->isVectorTy()) {
2566 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002567 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002568 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002570 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002572 } else if (DestTy->isVectorTy()) {
2573 assert(DestBits == SrcBits &&
2574 "Illegal cast to vector (wrong type or size)");
2575 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002576 } else if (DestTy->isPointerTy()) {
2577 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002578 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002579 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580 return IntToPtr; // int -> ptr
2581 } else {
Richard Trieua318b8d2011-09-21 03:09:09 +00002582 assert(0 && "Casting pointer to other than pointer or int");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002584 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002585 if (SrcTy->isVectorTy()) {
2586 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002587 return BitCast; // 64-bit vector to MMX
2588 } else {
Richard Trieua318b8d2011-09-21 03:09:09 +00002589 assert(0 && "Illegal cast to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002590 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591 } else {
Richard Trieua318b8d2011-09-21 03:09:09 +00002592 assert(0 && "Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593 }
2594
2595 // If we fall through to here we probably hit an assertion cast above
2596 // and assertions are not turned on. Anything we return is an error, so
2597 // BitCast is as good a choice as any.
2598 return BitCast;
2599}
2600
2601//===----------------------------------------------------------------------===//
2602// CastInst SubClass Constructors
2603//===----------------------------------------------------------------------===//
2604
2605/// Check that the construction parameters for a CastInst are correct. This
2606/// could be broken out into the separate constructors but it is useful to have
2607/// it in one place and to eliminate the redundant code for getting the sizes
2608/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002609bool
Chris Lattner229907c2011-07-18 04:54:35 +00002610CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611
2612 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002613 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002614 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2615 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616 return false;
2617
2618 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002619 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2620 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621
Duncan Sands7f646562011-05-18 09:21:57 +00002622 // If these are vector types, get the lengths of the vectors (using zero for
2623 // scalar types means that checking that vector lengths match also checks that
2624 // scalars are not being converted to vectors or vectors to scalars).
2625 unsigned SrcLength = SrcTy->isVectorTy() ?
2626 cast<VectorType>(SrcTy)->getNumElements() : 0;
2627 unsigned DstLength = DstTy->isVectorTy() ?
2628 cast<VectorType>(DstTy)->getNumElements() : 0;
2629
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630 // Switch on the opcode provided
2631 switch (op) {
2632 default: return false; // This is an input error
2633 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002634 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2635 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002637 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2638 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002639 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002640 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2641 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002643 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2644 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002646 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2647 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002649 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002650 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2651 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002654 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2655 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002656 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002657 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002658 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002659 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660 case Instruction::BitCast:
2661 // BitCast implies a no-op cast of type only. No bits change.
2662 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002663 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002664 return false;
2665
Duncan Sands55e50902008-01-06 10:12:28 +00002666 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002667 // these cases, the cast is okay if the source and destination bit widths
2668 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002669 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002670 }
2671}
2672
2673TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002674 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002675) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002676 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677}
2678
2679TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002680 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002682 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683}
2684
2685ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002686 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002688 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689}
2690
2691ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002692 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002693) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002694 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002695}
2696SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002697 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002698) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002699 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002700}
2701
Jeff Cohencc08c832006-12-02 02:22:01 +00002702SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002703 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002704) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002705 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002706}
2707
2708FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002709 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002710) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002711 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002712}
2713
2714FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002715 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002716) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002717 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002718}
2719
2720FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002721 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002722) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002723 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002724}
2725
2726FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002727 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002729 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002730}
2731
2732UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002733 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002734) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002735 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002736}
2737
2738UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002739 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002740) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002741 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002742}
2743
2744SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002745 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002746) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002747 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002748}
2749
2750SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002751 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002752) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002753 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002754}
2755
2756FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002757 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002758) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002759 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002760}
2761
2762FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002763 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002764) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002765 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002766}
2767
2768FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002769 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002770) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002771 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002772}
2773
2774FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002775 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002776) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002777 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002778}
2779
2780PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002781 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002782) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002783 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002784}
2785
2786PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002787 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002788) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002789 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002790}
2791
2792IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002793 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002794) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002795 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002796}
2797
2798IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002799 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002800) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002801 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002802}
2803
2804BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002805 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002806) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002807 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002808}
2809
2810BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002811 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002812) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002813 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002814}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002815
2816//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002817// CmpInst Classes
2818//===----------------------------------------------------------------------===//
2819
Chris Lattneraec33da2010-01-22 06:25:37 +00002820void CmpInst::Anchor() const {}
2821
Chris Lattner229907c2011-07-18 04:54:35 +00002822CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002823 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002824 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002825 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002826 OperandTraits<CmpInst>::op_begin(this),
2827 OperandTraits<CmpInst>::operands(this),
2828 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002829 Op<0>() = LHS;
2830 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002831 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002832 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002833}
Gabor Greiff6caff662008-05-10 08:32:32 +00002834
Chris Lattner229907c2011-07-18 04:54:35 +00002835CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002836 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002837 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002838 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002839 OperandTraits<CmpInst>::op_begin(this),
2840 OperandTraits<CmpInst>::operands(this),
2841 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002842 Op<0>() = LHS;
2843 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002844 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002845 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002846}
2847
2848CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002849CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002850 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002851 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002852 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002853 if (InsertBefore)
2854 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2855 S1, S2, Name);
2856 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002857 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002858 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002859 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002860
2861 if (InsertBefore)
2862 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2863 S1, S2, Name);
2864 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002865 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002866 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002867}
2868
2869CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002870CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002871 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002872 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002873 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2874 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002875 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002876 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2877 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002878}
2879
2880void CmpInst::swapOperands() {
2881 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2882 IC->swapOperands();
2883 else
2884 cast<FCmpInst>(this)->swapOperands();
2885}
2886
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002887bool CmpInst::isCommutative() const {
2888 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002889 return IC->isCommutative();
2890 return cast<FCmpInst>(this)->isCommutative();
2891}
2892
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002893bool CmpInst::isEquality() const {
2894 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002895 return IC->isEquality();
2896 return cast<FCmpInst>(this)->isEquality();
2897}
2898
2899
Dan Gohman4e724382008-05-31 02:47:54 +00002900CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002901 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002902 default: assert(0 && "Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002903 case ICMP_EQ: return ICMP_NE;
2904 case ICMP_NE: return ICMP_EQ;
2905 case ICMP_UGT: return ICMP_ULE;
2906 case ICMP_ULT: return ICMP_UGE;
2907 case ICMP_UGE: return ICMP_ULT;
2908 case ICMP_ULE: return ICMP_UGT;
2909 case ICMP_SGT: return ICMP_SLE;
2910 case ICMP_SLT: return ICMP_SGE;
2911 case ICMP_SGE: return ICMP_SLT;
2912 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002913
Dan Gohman4e724382008-05-31 02:47:54 +00002914 case FCMP_OEQ: return FCMP_UNE;
2915 case FCMP_ONE: return FCMP_UEQ;
2916 case FCMP_OGT: return FCMP_ULE;
2917 case FCMP_OLT: return FCMP_UGE;
2918 case FCMP_OGE: return FCMP_ULT;
2919 case FCMP_OLE: return FCMP_UGT;
2920 case FCMP_UEQ: return FCMP_ONE;
2921 case FCMP_UNE: return FCMP_OEQ;
2922 case FCMP_UGT: return FCMP_OLE;
2923 case FCMP_ULT: return FCMP_OGE;
2924 case FCMP_UGE: return FCMP_OLT;
2925 case FCMP_ULE: return FCMP_OGT;
2926 case FCMP_ORD: return FCMP_UNO;
2927 case FCMP_UNO: return FCMP_ORD;
2928 case FCMP_TRUE: return FCMP_FALSE;
2929 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002930 }
2931}
2932
Reid Spencer266e42b2006-12-23 06:05:41 +00002933ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2934 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002935 default: assert(0 && "Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002936 case ICMP_EQ: case ICMP_NE:
2937 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2938 return pred;
2939 case ICMP_UGT: return ICMP_SGT;
2940 case ICMP_ULT: return ICMP_SLT;
2941 case ICMP_UGE: return ICMP_SGE;
2942 case ICMP_ULE: return ICMP_SLE;
2943 }
2944}
2945
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002946ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2947 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002948 default: assert(0 && "Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002949 case ICMP_EQ: case ICMP_NE:
2950 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2951 return pred;
2952 case ICMP_SGT: return ICMP_UGT;
2953 case ICMP_SLT: return ICMP_ULT;
2954 case ICMP_SGE: return ICMP_UGE;
2955 case ICMP_SLE: return ICMP_ULE;
2956 }
2957}
2958
Reid Spencer0286bc12007-02-28 22:00:54 +00002959/// Initialize a set of values that all satisfy the condition with C.
2960///
2961ConstantRange
2962ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2963 APInt Lower(C);
2964 APInt Upper(C);
2965 uint32_t BitWidth = C.getBitWidth();
2966 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002967 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002968 case ICmpInst::ICMP_EQ: Upper++; break;
2969 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002970 case ICmpInst::ICMP_ULT:
2971 Lower = APInt::getMinValue(BitWidth);
2972 // Check for an empty-set condition.
2973 if (Lower == Upper)
2974 return ConstantRange(BitWidth, /*isFullSet=*/false);
2975 break;
2976 case ICmpInst::ICMP_SLT:
2977 Lower = APInt::getSignedMinValue(BitWidth);
2978 // Check for an empty-set condition.
2979 if (Lower == Upper)
2980 return ConstantRange(BitWidth, /*isFullSet=*/false);
2981 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002982 case ICmpInst::ICMP_UGT:
2983 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002984 // Check for an empty-set condition.
2985 if (Lower == Upper)
2986 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002987 break;
2988 case ICmpInst::ICMP_SGT:
2989 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002990 // Check for an empty-set condition.
2991 if (Lower == Upper)
2992 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002993 break;
2994 case ICmpInst::ICMP_ULE:
2995 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002996 // Check for a full-set condition.
2997 if (Lower == Upper)
2998 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002999 break;
3000 case ICmpInst::ICMP_SLE:
3001 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003002 // Check for a full-set condition.
3003 if (Lower == Upper)
3004 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003005 break;
3006 case ICmpInst::ICMP_UGE:
3007 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003008 // Check for a full-set condition.
3009 if (Lower == Upper)
3010 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003011 break;
3012 case ICmpInst::ICMP_SGE:
3013 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003014 // Check for a full-set condition.
3015 if (Lower == Upper)
3016 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003017 break;
3018 }
3019 return ConstantRange(Lower, Upper);
3020}
3021
Dan Gohman4e724382008-05-31 02:47:54 +00003022CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003023 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00003024 default: assert(0 && "Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003025 case ICMP_EQ: case ICMP_NE:
3026 return pred;
3027 case ICMP_SGT: return ICMP_SLT;
3028 case ICMP_SLT: return ICMP_SGT;
3029 case ICMP_SGE: return ICMP_SLE;
3030 case ICMP_SLE: return ICMP_SGE;
3031 case ICMP_UGT: return ICMP_ULT;
3032 case ICMP_ULT: return ICMP_UGT;
3033 case ICMP_UGE: return ICMP_ULE;
3034 case ICMP_ULE: return ICMP_UGE;
3035
Reid Spencerd9436b62006-11-20 01:22:35 +00003036 case FCMP_FALSE: case FCMP_TRUE:
3037 case FCMP_OEQ: case FCMP_ONE:
3038 case FCMP_UEQ: case FCMP_UNE:
3039 case FCMP_ORD: case FCMP_UNO:
3040 return pred;
3041 case FCMP_OGT: return FCMP_OLT;
3042 case FCMP_OLT: return FCMP_OGT;
3043 case FCMP_OGE: return FCMP_OLE;
3044 case FCMP_OLE: return FCMP_OGE;
3045 case FCMP_UGT: return FCMP_ULT;
3046 case FCMP_ULT: return FCMP_UGT;
3047 case FCMP_UGE: return FCMP_ULE;
3048 case FCMP_ULE: return FCMP_UGE;
3049 }
3050}
3051
Reid Spencer266e42b2006-12-23 06:05:41 +00003052bool CmpInst::isUnsigned(unsigned short predicate) {
3053 switch (predicate) {
3054 default: return false;
3055 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3056 case ICmpInst::ICMP_UGE: return true;
3057 }
3058}
3059
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003060bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003061 switch (predicate) {
3062 default: return false;
3063 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3064 case ICmpInst::ICMP_SGE: return true;
3065 }
3066}
3067
3068bool CmpInst::isOrdered(unsigned short predicate) {
3069 switch (predicate) {
3070 default: return false;
3071 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3072 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3073 case FCmpInst::FCMP_ORD: return true;
3074 }
3075}
3076
3077bool CmpInst::isUnordered(unsigned short predicate) {
3078 switch (predicate) {
3079 default: return false;
3080 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3081 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3082 case FCmpInst::FCMP_UNO: return true;
3083 }
3084}
3085
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003086bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3087 switch(predicate) {
3088 default: return false;
3089 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3090 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3091 }
3092}
3093
3094bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3095 switch(predicate) {
3096 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3097 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3098 default: return false;
3099 }
3100}
3101
3102
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003103//===----------------------------------------------------------------------===//
3104// SwitchInst Implementation
3105//===----------------------------------------------------------------------===//
3106
Chris Lattnerbaf00152010-11-17 05:41:46 +00003107void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3108 assert(Value && Default && NumReserved);
3109 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003110 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003111 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003112
Gabor Greif2d3024d2008-05-26 21:33:52 +00003113 OperandList[0] = Value;
3114 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003115}
3116
Chris Lattner2195fc42007-02-24 00:55:48 +00003117/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3118/// switch on and a default destination. The number of additional cases can
3119/// be specified here to make memory allocation more efficient. This
3120/// constructor can also autoinsert before another instruction.
3121SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3122 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003123 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3124 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003125 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003126}
3127
3128/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3129/// switch on and a default destination. The number of additional cases can
3130/// be specified here to make memory allocation more efficient. This
3131/// constructor also autoinserts at the end of the specified BasicBlock.
3132SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3133 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003134 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3135 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003136 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003137}
3138
Misha Brukmanb1c93172005-04-21 23:48:37 +00003139SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003140 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3141 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3142 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003143 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003144 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003145 OL[i] = InOL[i];
3146 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003147 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003148 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003149}
3150
Gordon Henriksen14a55692007-12-10 02:14:30 +00003151SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003152 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003153}
3154
3155
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003156/// addCase - Add an entry to the switch instruction...
3157///
Chris Lattner47ac1872005-02-24 05:32:09 +00003158void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003159 unsigned OpNo = NumOperands;
3160 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003161 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003162 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003163 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003164 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003165 OperandList[OpNo] = OnVal;
3166 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003167}
3168
3169/// removeCase - This method removes the specified successor from the switch
3170/// instruction. Note that this cannot be used to remove the default
3171/// destination (successor #0).
3172///
3173void SwitchInst::removeCase(unsigned idx) {
3174 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003175 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3176
3177 unsigned NumOps = getNumOperands();
3178 Use *OL = OperandList;
3179
Jay Foad14277722011-02-01 09:22:34 +00003180 // Overwrite this case with the end of the list.
3181 if ((idx + 1) * 2 != NumOps) {
3182 OL[idx * 2] = OL[NumOps - 2];
3183 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003184 }
3185
3186 // Nuke the last value.
3187 OL[NumOps-2].set(0);
3188 OL[NumOps-2+1].set(0);
3189 NumOperands = NumOps-2;
3190}
3191
Jay Foade98f29d2011-04-01 08:00:58 +00003192/// growOperands - grow operands - This grows the operand list in response
3193/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003194///
Jay Foade98f29d2011-04-01 08:00:58 +00003195void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003196 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003197 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003198
3199 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003200 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003201 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003202 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003203 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003204 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003205 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003206 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003207}
3208
3209
3210BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3211 return getSuccessor(idx);
3212}
3213unsigned SwitchInst::getNumSuccessorsV() const {
3214 return getNumSuccessors();
3215}
3216void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3217 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003218}
Chris Lattnerf22be932004-10-15 23:52:53 +00003219
Chris Lattner3ed871f2009-10-27 19:13:16 +00003220//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003221// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003222//===----------------------------------------------------------------------===//
3223
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003224void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003225 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003226 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003227 ReservedSpace = 1+NumDests;
3228 NumOperands = 1;
3229 OperandList = allocHungoffUses(ReservedSpace);
3230
3231 OperandList[0] = Address;
3232}
3233
3234
Jay Foade98f29d2011-04-01 08:00:58 +00003235/// growOperands - grow operands - This grows the operand list in response
3236/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003237///
Jay Foade98f29d2011-04-01 08:00:58 +00003238void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003239 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003240 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003241
3242 ReservedSpace = NumOps;
3243 Use *NewOps = allocHungoffUses(NumOps);
3244 Use *OldOps = OperandList;
3245 for (unsigned i = 0; i != e; ++i)
3246 NewOps[i] = OldOps[i];
3247 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003248 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003249}
3250
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003251IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3252 Instruction *InsertBefore)
3253: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003254 0, 0, InsertBefore) {
3255 init(Address, NumCases);
3256}
3257
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003258IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3259 BasicBlock *InsertAtEnd)
3260: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003261 0, 0, InsertAtEnd) {
3262 init(Address, NumCases);
3263}
3264
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003265IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3266 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003267 allocHungoffUses(IBI.getNumOperands()),
3268 IBI.getNumOperands()) {
3269 Use *OL = OperandList, *InOL = IBI.OperandList;
3270 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3271 OL[i] = InOL[i];
3272 SubclassOptionalData = IBI.SubclassOptionalData;
3273}
3274
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003275IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003276 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003277}
3278
3279/// addDestination - Add a destination.
3280///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003281void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003282 unsigned OpNo = NumOperands;
3283 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003284 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003285 // Initialize some new operands.
3286 assert(OpNo < ReservedSpace && "Growing didn't work!");
3287 NumOperands = OpNo+1;
3288 OperandList[OpNo] = DestBB;
3289}
3290
3291/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003292/// indirectbr instruction.
3293void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003294 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3295
3296 unsigned NumOps = getNumOperands();
3297 Use *OL = OperandList;
3298
3299 // Replace this value with the last one.
3300 OL[idx+1] = OL[NumOps-1];
3301
3302 // Nuke the last value.
3303 OL[NumOps-1].set(0);
3304 NumOperands = NumOps-1;
3305}
3306
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003307BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003308 return getSuccessor(idx);
3309}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003310unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003311 return getNumSuccessors();
3312}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003313void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003314 setSuccessor(idx, B);
3315}
3316
3317//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003318// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003319//===----------------------------------------------------------------------===//
3320
Chris Lattnerf22be932004-10-15 23:52:53 +00003321// Define these methods here so vtables don't get emitted into every translation
3322// unit that uses these classes.
3323
Devang Patel11cf3f42009-10-27 22:16:29 +00003324GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3325 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003326}
3327
Devang Patel11cf3f42009-10-27 22:16:29 +00003328BinaryOperator *BinaryOperator::clone_impl() const {
3329 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003330}
3331
Devang Patel11cf3f42009-10-27 22:16:29 +00003332FCmpInst* FCmpInst::clone_impl() const {
3333 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003334}
3335
Devang Patel11cf3f42009-10-27 22:16:29 +00003336ICmpInst* ICmpInst::clone_impl() const {
3337 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003338}
3339
Devang Patel11cf3f42009-10-27 22:16:29 +00003340ExtractValueInst *ExtractValueInst::clone_impl() const {
3341 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003342}
3343
Devang Patel11cf3f42009-10-27 22:16:29 +00003344InsertValueInst *InsertValueInst::clone_impl() const {
3345 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003346}
3347
Devang Patel11cf3f42009-10-27 22:16:29 +00003348AllocaInst *AllocaInst::clone_impl() const {
3349 return new AllocaInst(getAllocatedType(),
3350 (Value*)getOperand(0),
3351 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003352}
3353
Devang Patel11cf3f42009-10-27 22:16:29 +00003354LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003355 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3356 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003357}
3358
Devang Patel11cf3f42009-10-27 22:16:29 +00003359StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003360 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003361 getAlignment(), getOrdering(), getSynchScope());
3362
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003363}
3364
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003365AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3366 AtomicCmpXchgInst *Result =
3367 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3368 getOrdering(), getSynchScope());
3369 Result->setVolatile(isVolatile());
3370 return Result;
3371}
3372
3373AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3374 AtomicRMWInst *Result =
3375 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3376 getOrdering(), getSynchScope());
3377 Result->setVolatile(isVolatile());
3378 return Result;
3379}
3380
Eli Friedmanfee02c62011-07-25 23:16:38 +00003381FenceInst *FenceInst::clone_impl() const {
3382 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3383}
3384
Devang Patel11cf3f42009-10-27 22:16:29 +00003385TruncInst *TruncInst::clone_impl() const {
3386 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003387}
3388
Devang Patel11cf3f42009-10-27 22:16:29 +00003389ZExtInst *ZExtInst::clone_impl() const {
3390 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003391}
3392
Devang Patel11cf3f42009-10-27 22:16:29 +00003393SExtInst *SExtInst::clone_impl() const {
3394 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003395}
3396
Devang Patel11cf3f42009-10-27 22:16:29 +00003397FPTruncInst *FPTruncInst::clone_impl() const {
3398 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003399}
3400
Devang Patel11cf3f42009-10-27 22:16:29 +00003401FPExtInst *FPExtInst::clone_impl() const {
3402 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003403}
3404
Devang Patel11cf3f42009-10-27 22:16:29 +00003405UIToFPInst *UIToFPInst::clone_impl() const {
3406 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003407}
3408
Devang Patel11cf3f42009-10-27 22:16:29 +00003409SIToFPInst *SIToFPInst::clone_impl() const {
3410 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003411}
3412
Devang Patel11cf3f42009-10-27 22:16:29 +00003413FPToUIInst *FPToUIInst::clone_impl() const {
3414 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003415}
3416
Devang Patel11cf3f42009-10-27 22:16:29 +00003417FPToSIInst *FPToSIInst::clone_impl() const {
3418 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003419}
3420
Devang Patel11cf3f42009-10-27 22:16:29 +00003421PtrToIntInst *PtrToIntInst::clone_impl() const {
3422 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003423}
3424
Devang Patel11cf3f42009-10-27 22:16:29 +00003425IntToPtrInst *IntToPtrInst::clone_impl() const {
3426 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003427}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003428
Devang Patel11cf3f42009-10-27 22:16:29 +00003429BitCastInst *BitCastInst::clone_impl() const {
3430 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003431}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003432
Devang Patel11cf3f42009-10-27 22:16:29 +00003433CallInst *CallInst::clone_impl() const {
3434 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003435}
3436
Devang Patel11cf3f42009-10-27 22:16:29 +00003437SelectInst *SelectInst::clone_impl() const {
3438 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003439}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003440
Devang Patel11cf3f42009-10-27 22:16:29 +00003441VAArgInst *VAArgInst::clone_impl() const {
3442 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003443}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003444
Devang Patel11cf3f42009-10-27 22:16:29 +00003445ExtractElementInst *ExtractElementInst::clone_impl() const {
3446 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003447}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003448
Devang Patel11cf3f42009-10-27 22:16:29 +00003449InsertElementInst *InsertElementInst::clone_impl() const {
3450 return InsertElementInst::Create(getOperand(0),
3451 getOperand(1),
3452 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003453}
3454
Devang Patel11cf3f42009-10-27 22:16:29 +00003455ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3456 return new ShuffleVectorInst(getOperand(0),
3457 getOperand(1),
3458 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003459}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003460
Devang Patel11cf3f42009-10-27 22:16:29 +00003461PHINode *PHINode::clone_impl() const {
3462 return new PHINode(*this);
3463}
3464
Bill Wendlingfae14752011-08-12 20:24:12 +00003465LandingPadInst *LandingPadInst::clone_impl() const {
3466 return new LandingPadInst(*this);
3467}
3468
Devang Patel11cf3f42009-10-27 22:16:29 +00003469ReturnInst *ReturnInst::clone_impl() const {
3470 return new(getNumOperands()) ReturnInst(*this);
3471}
3472
3473BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003474 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003475}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003476
Devang Patel11cf3f42009-10-27 22:16:29 +00003477SwitchInst *SwitchInst::clone_impl() const {
3478 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003479}
3480
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003481IndirectBrInst *IndirectBrInst::clone_impl() const {
3482 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003483}
3484
3485
Devang Patel11cf3f42009-10-27 22:16:29 +00003486InvokeInst *InvokeInst::clone_impl() const {
3487 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003488}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003489
Bill Wendlingf891bf82011-07-31 06:30:59 +00003490ResumeInst *ResumeInst::clone_impl() const {
3491 return new(1) ResumeInst(*this);
3492}
3493
Devang Patel11cf3f42009-10-27 22:16:29 +00003494UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003495 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003496 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003497}
3498
Devang Patel11cf3f42009-10-27 22:16:29 +00003499UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003500 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003501 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003502}