blob: 1de7c01c0d501673ea05de05920fd08deb3c6572 [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}
629
Devang Patel59643e52008-02-23 00:35:18 +0000630ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000631}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633//===----------------------------------------------------------------------===//
634// UnwindInst Implementation
635//===----------------------------------------------------------------------===//
636
Owen Anderson55f1c092009-08-13 21:58:54 +0000637UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
638 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
639 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000640}
Owen Anderson55f1c092009-08-13 21:58:54 +0000641UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
642 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
643 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000644}
645
646
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647unsigned UnwindInst::getNumSuccessorsV() const {
648 return getNumSuccessors();
649}
650
651void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000652 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000653}
654
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000656 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657}
658
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000659//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000660// ResumeInst Implementation
661//===----------------------------------------------------------------------===//
662
663ResumeInst::ResumeInst(const ResumeInst &RI)
664 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
665 OperandTraits<ResumeInst>::op_begin(this), 1) {
666 Op<0>() = RI.Op<0>();
667}
668
669ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
670 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
671 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
672 Op<0>() = Exn;
673}
674
675ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
676 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
677 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
678 Op<0>() = Exn;
679}
680
681unsigned ResumeInst::getNumSuccessorsV() const {
682 return getNumSuccessors();
683}
684
685void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
686 llvm_unreachable("ResumeInst has no successors!");
687}
688
689BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
690 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000691}
692
693//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000694// UnreachableInst Implementation
695//===----------------------------------------------------------------------===//
696
Owen Anderson55f1c092009-08-13 21:58:54 +0000697UnreachableInst::UnreachableInst(LLVMContext &Context,
698 Instruction *InsertBefore)
699 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
700 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000701}
Owen Anderson55f1c092009-08-13 21:58:54 +0000702UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
703 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
704 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000705}
706
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000707unsigned UnreachableInst::getNumSuccessorsV() const {
708 return getNumSuccessors();
709}
710
711void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendlingad088e62011-07-30 05:42:50 +0000712 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713}
714
715BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendlingad088e62011-07-30 05:42:50 +0000716 llvm_unreachable("UnwindInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000717}
718
719//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720// BranchInst Implementation
721//===----------------------------------------------------------------------===//
722
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723void BranchInst::AssertOK() {
724 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000725 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000726 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727}
728
Chris Lattner2195fc42007-02-24 00:55:48 +0000729BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000730 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000731 OperandTraits<BranchInst>::op_end(this) - 1,
732 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000733 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000734 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000735}
736BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
737 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000738 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000739 OperandTraits<BranchInst>::op_end(this) - 3,
740 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000741 Op<-1>() = IfTrue;
742 Op<-2>() = IfFalse;
743 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000744#ifndef NDEBUG
745 AssertOK();
746#endif
747}
748
749BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000750 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000751 OperandTraits<BranchInst>::op_end(this) - 1,
752 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000753 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000754 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000755}
756
757BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
758 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000759 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000760 OperandTraits<BranchInst>::op_end(this) - 3,
761 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000762 Op<-1>() = IfTrue;
763 Op<-2>() = IfFalse;
764 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000765#ifndef NDEBUG
766 AssertOK();
767#endif
768}
769
770
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000772 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000773 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
774 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000775 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000776 if (BI.getNumOperands() != 1) {
777 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000778 Op<-3>() = BI.Op<-3>();
779 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000781 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000782}
783
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000784void BranchInst::swapSuccessors() {
785 assert(isConditional() &&
786 "Cannot swap successors of an unconditional branch");
787 Op<-1>().swap(Op<-2>());
788
789 // Update profile metadata if present and it matches our structural
790 // expectations.
791 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
792 if (!ProfileData || ProfileData->getNumOperands() != 3)
793 return;
794
795 // The first operand is the name. Fetch them backwards and build a new one.
796 Value *Ops[] = {
797 ProfileData->getOperand(0),
798 ProfileData->getOperand(2),
799 ProfileData->getOperand(1)
800 };
801 setMetadata(LLVMContext::MD_prof,
802 MDNode::get(ProfileData->getContext(), Ops));
803}
804
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000805BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
806 return getSuccessor(idx);
807}
808unsigned BranchInst::getNumSuccessorsV() const {
809 return getNumSuccessors();
810}
811void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
812 setSuccessor(idx, B);
813}
814
815
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000817// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818//===----------------------------------------------------------------------===//
819
Owen Andersonb6b25302009-07-14 23:09:55 +0000820static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000821 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000822 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000823 else {
824 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000825 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000826 assert(Amt->getType()->isIntegerTy() &&
827 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000828 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000829 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000830}
831
Chris Lattner229907c2011-07-18 04:54:35 +0000832AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000833 const Twine &Name, Instruction *InsertBefore)
834 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
835 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
836 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000837 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000838 setName(Name);
839}
840
Chris Lattner229907c2011-07-18 04:54:35 +0000841AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000842 const Twine &Name, BasicBlock *InsertAtEnd)
843 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
844 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
845 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000846 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000847 setName(Name);
848}
849
Chris Lattner229907c2011-07-18 04:54:35 +0000850AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000851 Instruction *InsertBefore)
852 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
853 getAISize(Ty->getContext(), 0), InsertBefore) {
854 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000855 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000856 setName(Name);
857}
858
Chris Lattner229907c2011-07-18 04:54:35 +0000859AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000860 BasicBlock *InsertAtEnd)
861 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
862 getAISize(Ty->getContext(), 0), InsertAtEnd) {
863 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000864 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000865 setName(Name);
866}
867
Chris Lattner229907c2011-07-18 04:54:35 +0000868AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000869 const Twine &Name, Instruction *InsertBefore)
870 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000871 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000872 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000873 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000874 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000875}
876
Chris Lattner229907c2011-07-18 04:54:35 +0000877AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000878 const Twine &Name, BasicBlock *InsertAtEnd)
879 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000880 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000881 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000882 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000883 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000884}
885
Gordon Henriksen14a55692007-12-10 02:14:30 +0000886// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000887AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000888}
889
Victor Hernandez8acf2952009-10-23 21:09:37 +0000890void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000891 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000892 assert(Align <= MaximumAlignment &&
893 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000894 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000895 assert(getAlignment() == Align && "Alignment representation error!");
896}
897
Victor Hernandez8acf2952009-10-23 21:09:37 +0000898bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000899 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000900 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000901 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000902}
903
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000904Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905 return getType()->getElementType();
906}
907
Chris Lattner8b291e62008-11-26 02:54:17 +0000908/// isStaticAlloca - Return true if this alloca is in the entry block of the
909/// function and is a constant size. If so, the code generator will fold it
910/// into the prolog/epilog code, so it is basically free.
911bool AllocaInst::isStaticAlloca() const {
912 // Must be constant size.
913 if (!isa<ConstantInt>(getArraySize())) return false;
914
915 // Must be in the entry block.
916 const BasicBlock *Parent = getParent();
917 return Parent == &Parent->getParent()->front();
918}
919
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000920//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000921// LoadInst Implementation
922//===----------------------------------------------------------------------===//
923
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000925 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000927 assert(!(isAtomic() && getAlignment() == 0) &&
928 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929}
930
Daniel Dunbar4975db62009-07-25 04:41:11 +0000931LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000933 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000934 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000935 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000936 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000937 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000938 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000939}
940
Daniel Dunbar4975db62009-07-25 04:41:11 +0000941LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000942 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000943 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000944 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000945 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000946 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000947 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000948 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000949}
950
Daniel Dunbar4975db62009-07-25 04:41:11 +0000951LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000952 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000953 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000954 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000955 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000956 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000957 setAtomic(NotAtomic);
958 AssertOK();
959 setName(Name);
960}
961
962LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
963 BasicBlock *InsertAE)
964 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
965 Load, Ptr, InsertAE) {
966 setVolatile(isVolatile);
967 setAlignment(0);
968 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +0000969 AssertOK();
970 setName(Name);
971}
972
Daniel Dunbar4975db62009-07-25 04:41:11 +0000973LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000974 unsigned Align, Instruction *InsertBef)
975 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
976 Load, Ptr, InsertBef) {
977 setVolatile(isVolatile);
978 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000979 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000980 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000981 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000982}
983
Daniel Dunbar4975db62009-07-25 04:41:11 +0000984LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000985 unsigned Align, BasicBlock *InsertAE)
986 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
987 Load, Ptr, InsertAE) {
988 setVolatile(isVolatile);
989 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000990 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +0000991 AssertOK();
992 setName(Name);
993}
994
Eli Friedman59b66882011-08-09 23:02:53 +0000995LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
996 unsigned Align, AtomicOrdering Order,
997 SynchronizationScope SynchScope,
998 Instruction *InsertBef)
999 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1000 Load, Ptr, InsertBef) {
1001 setVolatile(isVolatile);
1002 setAlignment(Align);
1003 setAtomic(Order, SynchScope);
1004 AssertOK();
1005 setName(Name);
1006}
1007
1008LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1009 unsigned Align, AtomicOrdering Order,
1010 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001011 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001012 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001013 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001014 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001015 setAlignment(Align);
1016 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001017 AssertOK();
1018 setName(Name);
1019}
1020
Daniel Dunbar27096822009-08-11 18:11:15 +00001021LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1022 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1023 Load, Ptr, InsertBef) {
1024 setVolatile(false);
1025 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001026 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001027 AssertOK();
1028 if (Name && Name[0]) setName(Name);
1029}
1030
1031LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1032 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1033 Load, Ptr, InsertAE) {
1034 setVolatile(false);
1035 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001036 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001037 AssertOK();
1038 if (Name && Name[0]) setName(Name);
1039}
1040
1041LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1042 Instruction *InsertBef)
1043: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1044 Load, Ptr, InsertBef) {
1045 setVolatile(isVolatile);
1046 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001047 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001048 AssertOK();
1049 if (Name && Name[0]) setName(Name);
1050}
1051
1052LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1053 BasicBlock *InsertAE)
1054 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1055 Load, Ptr, InsertAE) {
1056 setVolatile(isVolatile);
1057 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001058 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001059 AssertOK();
1060 if (Name && Name[0]) setName(Name);
1061}
1062
Christopher Lamb84485702007-04-22 19:24:39 +00001063void LoadInst::setAlignment(unsigned Align) {
1064 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001065 assert(Align <= MaximumAlignment &&
1066 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001067 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001068 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001069 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001070}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001071
1072//===----------------------------------------------------------------------===//
1073// StoreInst Implementation
1074//===----------------------------------------------------------------------===//
1075
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001076void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001077 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001078 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001079 "Ptr must have pointer type!");
1080 assert(getOperand(0)->getType() ==
1081 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001082 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001083 assert(!(isAtomic() && getAlignment() == 0) &&
1084 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001085}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001086
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001087
1088StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001089 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001090 OperandTraits<StoreInst>::op_begin(this),
1091 OperandTraits<StoreInst>::operands(this),
1092 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001093 Op<0>() = val;
1094 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001095 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001096 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001097 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001098 AssertOK();
1099}
1100
1101StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001102 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001103 OperandTraits<StoreInst>::op_begin(this),
1104 OperandTraits<StoreInst>::operands(this),
1105 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001106 Op<0>() = val;
1107 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001108 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001109 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001110 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001111 AssertOK();
1112}
1113
Misha Brukmanb1c93172005-04-21 23:48:37 +00001114StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001115 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001116 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001117 OperandTraits<StoreInst>::op_begin(this),
1118 OperandTraits<StoreInst>::operands(this),
1119 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001120 Op<0>() = val;
1121 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001122 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001123 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001124 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001125 AssertOK();
1126}
1127
1128StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1129 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001130 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001131 OperandTraits<StoreInst>::op_begin(this),
1132 OperandTraits<StoreInst>::operands(this),
1133 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001134 Op<0>() = val;
1135 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001136 setVolatile(isVolatile);
1137 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001138 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001139 AssertOK();
1140}
1141
Misha Brukmanb1c93172005-04-21 23:48:37 +00001142StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001143 unsigned Align, AtomicOrdering Order,
1144 SynchronizationScope SynchScope,
1145 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001146 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001147 OperandTraits<StoreInst>::op_begin(this),
1148 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001149 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001150 Op<0>() = val;
1151 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001152 setVolatile(isVolatile);
1153 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001154 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001155 AssertOK();
1156}
1157
1158StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001159 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001160 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001161 OperandTraits<StoreInst>::op_begin(this),
1162 OperandTraits<StoreInst>::operands(this),
1163 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001164 Op<0>() = val;
1165 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001166 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001167 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001168 setAtomic(NotAtomic);
1169 AssertOK();
1170}
1171
1172StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1173 unsigned Align, BasicBlock *InsertAtEnd)
1174 : Instruction(Type::getVoidTy(val->getContext()), Store,
1175 OperandTraits<StoreInst>::op_begin(this),
1176 OperandTraits<StoreInst>::operands(this),
1177 InsertAtEnd) {
1178 Op<0>() = val;
1179 Op<1>() = addr;
1180 setVolatile(isVolatile);
1181 setAlignment(Align);
1182 setAtomic(NotAtomic);
1183 AssertOK();
1184}
1185
1186StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1187 unsigned Align, AtomicOrdering Order,
1188 SynchronizationScope SynchScope,
1189 BasicBlock *InsertAtEnd)
1190 : Instruction(Type::getVoidTy(val->getContext()), Store,
1191 OperandTraits<StoreInst>::op_begin(this),
1192 OperandTraits<StoreInst>::operands(this),
1193 InsertAtEnd) {
1194 Op<0>() = val;
1195 Op<1>() = addr;
1196 setVolatile(isVolatile);
1197 setAlignment(Align);
1198 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001199 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001200}
1201
Christopher Lamb84485702007-04-22 19:24:39 +00001202void StoreInst::setAlignment(unsigned Align) {
1203 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001204 assert(Align <= MaximumAlignment &&
1205 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001206 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001207 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001208 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001209}
1210
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001211//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001212// AtomicCmpXchgInst Implementation
1213//===----------------------------------------------------------------------===//
1214
1215void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1216 AtomicOrdering Ordering,
1217 SynchronizationScope SynchScope) {
1218 Op<0>() = Ptr;
1219 Op<1>() = Cmp;
1220 Op<2>() = NewVal;
1221 setOrdering(Ordering);
1222 setSynchScope(SynchScope);
1223
1224 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1225 "All operands must be non-null!");
1226 assert(getOperand(0)->getType()->isPointerTy() &&
1227 "Ptr must have pointer type!");
1228 assert(getOperand(1)->getType() ==
1229 cast<PointerType>(getOperand(0)->getType())->getElementType()
1230 && "Ptr must be a pointer to Cmp type!");
1231 assert(getOperand(2)->getType() ==
1232 cast<PointerType>(getOperand(0)->getType())->getElementType()
1233 && "Ptr must be a pointer to NewVal type!");
1234 assert(Ordering != NotAtomic &&
1235 "AtomicCmpXchg instructions must be atomic!");
1236}
1237
1238AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1239 AtomicOrdering Ordering,
1240 SynchronizationScope SynchScope,
1241 Instruction *InsertBefore)
1242 : Instruction(Cmp->getType(), AtomicCmpXchg,
1243 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1244 OperandTraits<AtomicCmpXchgInst>::operands(this),
1245 InsertBefore) {
1246 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1247}
1248
1249AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1250 AtomicOrdering Ordering,
1251 SynchronizationScope SynchScope,
1252 BasicBlock *InsertAtEnd)
1253 : Instruction(Cmp->getType(), AtomicCmpXchg,
1254 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1255 OperandTraits<AtomicCmpXchgInst>::operands(this),
1256 InsertAtEnd) {
1257 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1258}
1259
1260//===----------------------------------------------------------------------===//
1261// AtomicRMWInst Implementation
1262//===----------------------------------------------------------------------===//
1263
1264void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1265 AtomicOrdering Ordering,
1266 SynchronizationScope SynchScope) {
1267 Op<0>() = Ptr;
1268 Op<1>() = Val;
1269 setOperation(Operation);
1270 setOrdering(Ordering);
1271 setSynchScope(SynchScope);
1272
1273 assert(getOperand(0) && getOperand(1) &&
1274 "All operands must be non-null!");
1275 assert(getOperand(0)->getType()->isPointerTy() &&
1276 "Ptr must have pointer type!");
1277 assert(getOperand(1)->getType() ==
1278 cast<PointerType>(getOperand(0)->getType())->getElementType()
1279 && "Ptr must be a pointer to Val type!");
1280 assert(Ordering != NotAtomic &&
1281 "AtomicRMW instructions must be atomic!");
1282}
1283
1284AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1285 AtomicOrdering Ordering,
1286 SynchronizationScope SynchScope,
1287 Instruction *InsertBefore)
1288 : Instruction(Val->getType(), AtomicRMW,
1289 OperandTraits<AtomicRMWInst>::op_begin(this),
1290 OperandTraits<AtomicRMWInst>::operands(this),
1291 InsertBefore) {
1292 Init(Operation, Ptr, Val, Ordering, SynchScope);
1293}
1294
1295AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1296 AtomicOrdering Ordering,
1297 SynchronizationScope SynchScope,
1298 BasicBlock *InsertAtEnd)
1299 : Instruction(Val->getType(), AtomicRMW,
1300 OperandTraits<AtomicRMWInst>::op_begin(this),
1301 OperandTraits<AtomicRMWInst>::operands(this),
1302 InsertAtEnd) {
1303 Init(Operation, Ptr, Val, Ordering, SynchScope);
1304}
1305
1306//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001307// FenceInst Implementation
1308//===----------------------------------------------------------------------===//
1309
1310FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1311 SynchronizationScope SynchScope,
1312 Instruction *InsertBefore)
1313 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1314 setOrdering(Ordering);
1315 setSynchScope(SynchScope);
1316}
1317
1318FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1319 SynchronizationScope SynchScope,
1320 BasicBlock *InsertAtEnd)
1321 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1322 setOrdering(Ordering);
1323 setSynchScope(SynchScope);
1324}
1325
1326//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001327// GetElementPtrInst Implementation
1328//===----------------------------------------------------------------------===//
1329
Jay Foadd1b78492011-07-25 09:48:08 +00001330void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001331 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001332 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1333 OperandList[0] = Ptr;
1334 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001335 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001336}
1337
Gabor Greiff6caff662008-05-10 08:32:32 +00001338GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001339 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001340 OperandTraits<GetElementPtrInst>::op_end(this)
1341 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001342 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001343 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001344 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001345}
1346
Chris Lattner6090a422009-03-09 04:46:40 +00001347/// getIndexedType - Returns the type of the element that would be accessed with
1348/// a gep instruction with the specified parameters.
1349///
1350/// The Idxs pointer should point to a continuous piece of memory containing the
1351/// indices, either as Value* or uint64_t.
1352///
1353/// A null type is returned if the indices are invalid for the specified
1354/// pointer type.
1355///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001356template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001357static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001358 if (Ptr->isVectorTy()) {
1359 assert(IdxList.size() == 1 &&
1360 "GEP with vector pointers must have a single index");
1361 PointerType *PTy = dyn_cast<PointerType>(
1362 cast<VectorType>(Ptr)->getElementType());
1363 assert(PTy && "Gep with invalid vector pointer found");
1364 return PTy->getElementType();
1365 }
1366
Chris Lattner229907c2011-07-18 04:54:35 +00001367 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001368 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001369 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001370
Chris Lattner6090a422009-03-09 04:46:40 +00001371 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001372 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001373 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001374
Chris Lattner6090a422009-03-09 04:46:40 +00001375 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001376 // it cannot be 'stepped over'.
1377 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001378 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001379
Dan Gohman1ecaf452008-05-31 00:58:22 +00001380 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001381 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001382 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001383 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001384 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001385 if (!CT->indexValid(Index)) return 0;
1386 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001387 }
Jay Foadd1b78492011-07-25 09:48:08 +00001388 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001389}
1390
Jay Foadd1b78492011-07-25 09:48:08 +00001391Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1392 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001393}
1394
Chris Lattner229907c2011-07-18 04:54:35 +00001395Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001396 ArrayRef<Constant *> IdxList) {
1397 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001398}
1399
Jay Foadd1b78492011-07-25 09:48:08 +00001400Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1401 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001402}
1403
Nadav Rotem3924cb02011-12-05 06:29:09 +00001404unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) {
1405 Type *Ty = Ptr->getType();
1406
1407 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1408 Ty = VTy->getElementType();
1409
1410 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
1411 return PTy->getAddressSpace();
1412
1413 assert(false && "Invalid GEP pointer type");
1414 return 0;
1415}
1416
Chris Lattner45f15572007-04-14 00:12:57 +00001417/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1418/// zeros. If so, the result pointer and the first operand have the same
1419/// value, just potentially different types.
1420bool GetElementPtrInst::hasAllZeroIndices() const {
1421 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1422 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1423 if (!CI->isZero()) return false;
1424 } else {
1425 return false;
1426 }
1427 }
1428 return true;
1429}
1430
Chris Lattner27058292007-04-27 20:35:56 +00001431/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1432/// constant integers. If so, the result pointer and the first operand have
1433/// a constant offset between them.
1434bool GetElementPtrInst::hasAllConstantIndices() const {
1435 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1436 if (!isa<ConstantInt>(getOperand(i)))
1437 return false;
1438 }
1439 return true;
1440}
1441
Dan Gohman1b849082009-09-07 23:54:19 +00001442void GetElementPtrInst::setIsInBounds(bool B) {
1443 cast<GEPOperator>(this)->setIsInBounds(B);
1444}
Chris Lattner45f15572007-04-14 00:12:57 +00001445
Nick Lewycky28a5f252009-09-27 21:33:04 +00001446bool GetElementPtrInst::isInBounds() const {
1447 return cast<GEPOperator>(this)->isInBounds();
1448}
1449
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001450//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001451// ExtractElementInst Implementation
1452//===----------------------------------------------------------------------===//
1453
1454ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001455 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001456 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001457 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001458 ExtractElement,
1459 OperandTraits<ExtractElementInst>::op_begin(this),
1460 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001461 assert(isValidOperands(Val, Index) &&
1462 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001463 Op<0>() = Val;
1464 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001465 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001466}
1467
1468ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001469 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001470 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001471 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001472 ExtractElement,
1473 OperandTraits<ExtractElementInst>::op_begin(this),
1474 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001475 assert(isValidOperands(Val, Index) &&
1476 "Invalid extractelement instruction operands!");
1477
Gabor Greif2d3024d2008-05-26 21:33:52 +00001478 Op<0>() = Val;
1479 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001480 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001481}
1482
Chris Lattner65511ff2006-10-05 06:24:58 +00001483
Chris Lattner54865b32006-04-08 04:05:48 +00001484bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001485 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001486 return false;
1487 return true;
1488}
1489
1490
Robert Bocchino23004482006-01-10 19:05:34 +00001491//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001492// InsertElementInst Implementation
1493//===----------------------------------------------------------------------===//
1494
Chris Lattner54865b32006-04-08 04:05:48 +00001495InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001496 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001497 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001498 : Instruction(Vec->getType(), InsertElement,
1499 OperandTraits<InsertElementInst>::op_begin(this),
1500 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001501 assert(isValidOperands(Vec, Elt, Index) &&
1502 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001503 Op<0>() = Vec;
1504 Op<1>() = Elt;
1505 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001506 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001507}
1508
Chris Lattner54865b32006-04-08 04:05:48 +00001509InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001510 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001511 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001512 : Instruction(Vec->getType(), InsertElement,
1513 OperandTraits<InsertElementInst>::op_begin(this),
1514 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001515 assert(isValidOperands(Vec, Elt, Index) &&
1516 "Invalid insertelement instruction operands!");
1517
Gabor Greif2d3024d2008-05-26 21:33:52 +00001518 Op<0>() = Vec;
1519 Op<1>() = Elt;
1520 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001521 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001522}
1523
Chris Lattner54865b32006-04-08 04:05:48 +00001524bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1525 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001526 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001527 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001528
Reid Spencerd84d35b2007-02-15 02:26:10 +00001529 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001530 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001531
Duncan Sands9dff9be2010-02-15 16:12:20 +00001532 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001533 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001534 return true;
1535}
1536
1537
Robert Bocchinoca27f032006-01-17 20:07:22 +00001538//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001539// ShuffleVectorInst Implementation
1540//===----------------------------------------------------------------------===//
1541
1542ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001543 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001544 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001545: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001546 cast<VectorType>(Mask->getType())->getNumElements()),
1547 ShuffleVector,
1548 OperandTraits<ShuffleVectorInst>::op_begin(this),
1549 OperandTraits<ShuffleVectorInst>::operands(this),
1550 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001551 assert(isValidOperands(V1, V2, Mask) &&
1552 "Invalid shuffle vector instruction operands!");
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
1559ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001560 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001561 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001562: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1563 cast<VectorType>(Mask->getType())->getNumElements()),
1564 ShuffleVector,
1565 OperandTraits<ShuffleVectorInst>::op_begin(this),
1566 OperandTraits<ShuffleVectorInst>::operands(this),
1567 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001568 assert(isValidOperands(V1, V2, Mask) &&
1569 "Invalid shuffle vector instruction operands!");
1570
Gabor Greif2d3024d2008-05-26 21:33:52 +00001571 Op<0>() = V1;
1572 Op<1>() = V2;
1573 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001574 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001575}
1576
Mon P Wang25f01062008-11-10 04:46:22 +00001577bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001578 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001579 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001580 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001581 return false;
1582
Chris Lattner1dcb6542012-01-25 23:49:49 +00001583 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001584 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001585 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001586 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001587
1588 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001589 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1590 return true;
1591
Nate Begeman60a31c32010-08-13 00:16:46 +00001592 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001593 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001594 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001595 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1596 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001597 return false;
1598 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1599 return false;
1600 }
1601 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001602 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001603 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001604
1605 if (const ConstantDataSequential *CDS =
1606 dyn_cast<ConstantDataSequential>(Mask)) {
1607 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1608 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1609 if (CDS->getElementAsInteger(i) >= V1Size*2)
1610 return false;
1611 return true;
1612 }
1613
1614 // The bitcode reader can create a place holder for a forward reference
1615 // used as the shuffle mask. When this occurs, the shuffle mask will
1616 // fall into this case and fail. To avoid this error, do this bit of
1617 // ugliness to allow such a mask pass.
1618 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1619 if (CE->getOpcode() == Instruction::UserOp1)
1620 return true;
1621
1622 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001623}
1624
Chris Lattnerf724e342008-03-02 05:28:33 +00001625/// getMaskValue - Return the index from the shuffle mask for the specified
1626/// output result. This is either -1 if the element is undef or a number less
1627/// than 2*numelements.
1628int ShuffleVectorInst::getMaskValue(unsigned i) const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001629 assert(i < getType()->getNumElements() && "Index out of range");
1630 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(getMask()))
1631 return CDS->getElementAsInteger(i);
1632 Constant *C = getMask()->getAggregateElement(i);
1633 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001634 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001635 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001636}
1637
Chris Lattner1dcb6542012-01-25 23:49:49 +00001638/// getShuffleMask - Return the full mask for this instruction, where each
1639/// element is the element number and undef's are returned as -1.
1640void ShuffleVectorInst::getShuffleMask(SmallVectorImpl<int> &Result) const {
1641 unsigned NumElts = getType()->getNumElements();
1642
1643 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(getMask())) {
1644 for (unsigned i = 0; i != NumElts; ++i)
1645 Result.push_back(CDS->getElementAsInteger(i));
1646 return;
1647 }
1648 Constant *Mask = getMask();
1649 for (unsigned i = 0; i != NumElts; ++i) {
1650 Constant *C = Mask->getAggregateElement(i);
1651 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001652 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001653 }
1654}
1655
1656
Dan Gohman12fce772008-05-15 19:50:34 +00001657//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001658// InsertValueInst Class
1659//===----------------------------------------------------------------------===//
1660
Jay Foad57aa6362011-07-13 10:26:04 +00001661void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1662 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001663 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001664
1665 // There's no fundamental reason why we require at least one index
1666 // (other than weirdness with &*IdxBegin being invalid; see
1667 // getelementptr's init routine for example). But there's no
1668 // present need to support it.
1669 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1670
1671 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001672 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001673 Op<0>() = Agg;
1674 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001675
Jay Foad57aa6362011-07-13 10:26:04 +00001676 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001677 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001678}
1679
1680InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001681 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001682 OperandTraits<InsertValueInst>::op_begin(this), 2),
1683 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001684 Op<0>() = IVI.getOperand(0);
1685 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001686 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001687}
1688
1689//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001690// ExtractValueInst Class
1691//===----------------------------------------------------------------------===//
1692
Jay Foad57aa6362011-07-13 10:26:04 +00001693void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001694 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001695
Jay Foad57aa6362011-07-13 10:26:04 +00001696 // There's no fundamental reason why we require at least one index.
1697 // But there's no present need to support it.
1698 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001699
Jay Foad57aa6362011-07-13 10:26:04 +00001700 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001701 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001702}
1703
1704ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001705 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001706 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001707 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001708}
1709
Dan Gohman12fce772008-05-15 19:50:34 +00001710// getIndexedType - Returns the type of the element that would be extracted
1711// with an extractvalue instruction with the specified parameters.
1712//
1713// A null type is returned if the indices are invalid for the specified
1714// pointer type.
1715//
Chris Lattner229907c2011-07-18 04:54:35 +00001716Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001717 ArrayRef<unsigned> Idxs) {
1718 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001719 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001720 // We can't use CompositeType::indexValid(Index) here.
1721 // indexValid() always returns true for arrays because getelementptr allows
1722 // out-of-bounds indices. Since we don't allow those for extractvalue and
1723 // insertvalue we need to check array indexing manually.
1724 // Since the only other types we can index into are struct types it's just
1725 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001726 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001727 if (Index >= AT->getNumElements())
1728 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001729 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001730 if (Index >= ST->getNumElements())
1731 return 0;
1732 } else {
1733 // Not a valid type to index into.
1734 return 0;
1735 }
1736
1737 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001738 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001739 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001740}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001741
1742//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001743// BinaryOperator Class
1744//===----------------------------------------------------------------------===//
1745
Chris Lattner2195fc42007-02-24 00:55:48 +00001746BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001747 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001748 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001749 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001750 OperandTraits<BinaryOperator>::op_begin(this),
1751 OperandTraits<BinaryOperator>::operands(this),
1752 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001753 Op<0>() = S1;
1754 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001755 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001756 setName(Name);
1757}
1758
1759BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001760 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001761 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001762 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001763 OperandTraits<BinaryOperator>::op_begin(this),
1764 OperandTraits<BinaryOperator>::operands(this),
1765 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001766 Op<0>() = S1;
1767 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001768 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001769 setName(Name);
1770}
1771
1772
1773void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001774 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001775 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001776 assert(LHS->getType() == RHS->getType() &&
1777 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001778#ifndef NDEBUG
1779 switch (iType) {
1780 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001781 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001782 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001783 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001784 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001785 "Tried to create an integer operation on a non-integer type!");
1786 break;
1787 case FAdd: case FSub:
1788 case FMul:
1789 assert(getType() == LHS->getType() &&
1790 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001791 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001792 "Tried to create a floating-point operation on a "
1793 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001794 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001795 case UDiv:
1796 case SDiv:
1797 assert(getType() == LHS->getType() &&
1798 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001799 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001800 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001801 "Incorrect operand type (not integer) for S/UDIV");
1802 break;
1803 case FDiv:
1804 assert(getType() == LHS->getType() &&
1805 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001806 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001807 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001808 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001809 case URem:
1810 case SRem:
1811 assert(getType() == LHS->getType() &&
1812 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001813 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001814 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001815 "Incorrect operand type (not integer) for S/UREM");
1816 break;
1817 case FRem:
1818 assert(getType() == LHS->getType() &&
1819 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001820 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001821 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001822 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001823 case Shl:
1824 case LShr:
1825 case AShr:
1826 assert(getType() == LHS->getType() &&
1827 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001828 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001829 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001830 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001831 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001832 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001833 case And: case Or:
1834 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001835 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001836 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001837 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001838 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001839 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001840 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001841 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001842 default:
1843 break;
1844 }
1845#endif
1846}
1847
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001848BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001849 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001850 Instruction *InsertBefore) {
1851 assert(S1->getType() == S2->getType() &&
1852 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001853 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001854}
1855
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001856BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001857 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001858 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001859 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001860 InsertAtEnd->getInstList().push_back(Res);
1861 return Res;
1862}
1863
Dan Gohman5476cfd2009-08-12 16:23:25 +00001864BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001865 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001866 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001867 return new BinaryOperator(Instruction::Sub,
1868 zero, Op,
1869 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001870}
1871
Dan Gohman5476cfd2009-08-12 16:23:25 +00001872BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001873 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001874 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001875 return new BinaryOperator(Instruction::Sub,
1876 zero, Op,
1877 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001878}
1879
Dan Gohman4ab44202009-12-18 02:58:50 +00001880BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1881 Instruction *InsertBefore) {
1882 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1883 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1884}
1885
1886BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1887 BasicBlock *InsertAtEnd) {
1888 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1889 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1890}
1891
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001892BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1893 Instruction *InsertBefore) {
1894 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1895 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1896}
1897
1898BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1899 BasicBlock *InsertAtEnd) {
1900 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1901 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1902}
1903
Dan Gohman5476cfd2009-08-12 16:23:25 +00001904BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001905 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001906 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001907 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001908 Op->getType(), Name, InsertBefore);
1909}
1910
Dan Gohman5476cfd2009-08-12 16:23:25 +00001911BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001912 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001913 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001914 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001915 Op->getType(), Name, InsertAtEnd);
1916}
1917
Dan Gohman5476cfd2009-08-12 16:23:25 +00001918BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001919 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001920 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001921 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001922 Op->getType(), Name, InsertBefore);
1923}
1924
Dan Gohman5476cfd2009-08-12 16:23:25 +00001925BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001926 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001927 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001928 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001929 Op->getType(), Name, InsertAtEnd);
1930}
1931
1932
1933// isConstantAllOnes - Helper function for several functions below
1934static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001935 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1936 return CI->isAllOnesValue();
1937 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1938 return CV->isAllOnesValue();
1939 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001940}
1941
Owen Andersonbb2501b2009-07-13 22:18:28 +00001942bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001943 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1944 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001945 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1946 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001947 return false;
1948}
1949
Owen Andersonbb2501b2009-07-13 22:18:28 +00001950bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001951 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1952 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001953 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001954 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001955 return false;
1956}
1957
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001958bool BinaryOperator::isNot(const Value *V) {
1959 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1960 return (Bop->getOpcode() == Instruction::Xor &&
1961 (isConstantAllOnes(Bop->getOperand(1)) ||
1962 isConstantAllOnes(Bop->getOperand(0))));
1963 return false;
1964}
1965
Chris Lattner2c7d1772005-04-24 07:28:37 +00001966Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001967 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001968}
1969
Chris Lattner2c7d1772005-04-24 07:28:37 +00001970const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1971 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001972}
1973
Dan Gohmana5b96452009-06-04 22:49:04 +00001974Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001975 return cast<BinaryOperator>(BinOp)->getOperand(1);
1976}
1977
1978const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1979 return getFNegArgument(const_cast<Value*>(BinOp));
1980}
1981
Chris Lattner2c7d1772005-04-24 07:28:37 +00001982Value *BinaryOperator::getNotArgument(Value *BinOp) {
1983 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1984 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1985 Value *Op0 = BO->getOperand(0);
1986 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001987 if (isConstantAllOnes(Op0)) return Op1;
1988
1989 assert(isConstantAllOnes(Op1));
1990 return Op0;
1991}
1992
Chris Lattner2c7d1772005-04-24 07:28:37 +00001993const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1994 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001995}
1996
1997
1998// swapOperands - Exchange the two operands to this instruction. This
1999// instruction is safe to use on any binary instruction and does not
2000// modify the semantics of the instruction. If the instruction is
2001// order dependent (SetLT f.e.) the opcode is changed.
2002//
2003bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002004 if (!isCommutative())
2005 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002006 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002007 return false;
2008}
2009
Dan Gohman1b849082009-09-07 23:54:19 +00002010void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2011 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2012}
2013
2014void BinaryOperator::setHasNoSignedWrap(bool b) {
2015 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2016}
2017
2018void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002019 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002020}
2021
Nick Lewycky28a5f252009-09-27 21:33:04 +00002022bool BinaryOperator::hasNoUnsignedWrap() const {
2023 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2024}
2025
2026bool BinaryOperator::hasNoSignedWrap() const {
2027 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2028}
2029
2030bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002031 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002032}
2033
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002034//===----------------------------------------------------------------------===//
2035// CastInst Class
2036//===----------------------------------------------------------------------===//
2037
David Blaikie3a15e142011-12-01 08:00:17 +00002038void CastInst::anchor() {}
2039
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002040// Just determine if this cast only deals with integral->integral conversion.
2041bool CastInst::isIntegerCast() const {
2042 switch (getOpcode()) {
2043 default: return false;
2044 case Instruction::ZExt:
2045 case Instruction::SExt:
2046 case Instruction::Trunc:
2047 return true;
2048 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002049 return getOperand(0)->getType()->isIntegerTy() &&
2050 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002051 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002052}
2053
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002054bool CastInst::isLosslessCast() const {
2055 // Only BitCast can be lossless, exit fast if we're not BitCast
2056 if (getOpcode() != Instruction::BitCast)
2057 return false;
2058
2059 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002060 Type* SrcTy = getOperand(0)->getType();
2061 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002062 if (SrcTy == DstTy)
2063 return true;
2064
Reid Spencer8d9336d2006-12-31 05:26:44 +00002065 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002066 if (SrcTy->isPointerTy())
2067 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068 return false; // Other types have no identity values
2069}
2070
2071/// This function determines if the CastInst does not require any bits to be
2072/// changed in order to effect the cast. Essentially, it identifies cases where
2073/// no code gen is necessary for the cast, hence the name no-op cast. For
2074/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002075/// # bitcast i32* %x to i8*
2076/// # bitcast <2 x i32> %x to <4 x i16>
2077/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002078/// @brief Determine if the described cast is a no-op.
2079bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002080 Type *SrcTy,
2081 Type *DestTy,
2082 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002083 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002084 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002085 assert(0 && "Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002086 case Instruction::Trunc:
2087 case Instruction::ZExt:
2088 case Instruction::SExt:
2089 case Instruction::FPTrunc:
2090 case Instruction::FPExt:
2091 case Instruction::UIToFP:
2092 case Instruction::SIToFP:
2093 case Instruction::FPToUI:
2094 case Instruction::FPToSI:
2095 return false; // These always modify bits
2096 case Instruction::BitCast:
2097 return true; // BitCast never modifies bits.
2098 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002099 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002100 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002101 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002102 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002103 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002104 }
2105}
2106
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002107/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002108bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002109 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2110}
2111
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112/// This function determines if a pair of casts can be eliminated and what
2113/// opcode should be used in the elimination. This assumes that there are two
2114/// instructions like this:
2115/// * %F = firstOpcode SrcTy %x to MidTy
2116/// * %S = secondOpcode MidTy %F to DstTy
2117/// The function returns a resultOpcode so these two casts can be replaced with:
2118/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2119/// If no such cast is permited, the function returns 0.
2120unsigned CastInst::isEliminableCastPair(
2121 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002122 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002123 // Define the 144 possibilities for these two cast instructions. The values
2124 // in this matrix determine what to do in a given situation and select the
2125 // case in the switch below. The rows correspond to firstOp, the columns
2126 // correspond to secondOp. In looking at the table below, keep in mind
2127 // the following cast properties:
2128 //
2129 // Size Compare Source Destination
2130 // Operator Src ? Size Type Sign Type Sign
2131 // -------- ------------ ------------------- ---------------------
2132 // TRUNC > Integer Any Integral Any
2133 // ZEXT < Integral Unsigned Integer Any
2134 // SEXT < Integral Signed Integer Any
2135 // FPTOUI n/a FloatPt n/a Integral Unsigned
2136 // FPTOSI n/a FloatPt n/a Integral Signed
2137 // UITOFP n/a Integral Unsigned FloatPt n/a
2138 // SITOFP n/a Integral Signed FloatPt n/a
2139 // FPTRUNC > FloatPt n/a FloatPt n/a
2140 // FPEXT < FloatPt n/a FloatPt n/a
2141 // PTRTOINT n/a Pointer n/a Integral Unsigned
2142 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002143 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002144 //
2145 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002146 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2147 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002148 // of the produced value (we no longer know the top-part is all zeros).
2149 // Further this conversion is often much more expensive for typical hardware,
2150 // and causes issues when building libgcc. We disallow fptosi+sext for the
2151 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002152 const unsigned numCastOps =
2153 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2154 static const uint8_t CastResults[numCastOps][numCastOps] = {
2155 // T F F U S F F P I B -+
2156 // R Z S P P I I T P 2 N T |
2157 // U E E 2 2 2 2 R E I T C +- secondOp
2158 // N X X U S F F N X N 2 V |
2159 // C T T I I P P C T T P T -+
2160 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2161 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2162 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002163 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2164 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2166 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2167 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2168 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2169 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2170 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2171 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2172 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002173
2174 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002175 // merging. However, bitcast of A->B->A are allowed.
2176 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2177 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2178 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2179
2180 // Check if any of the bitcasts convert scalars<->vectors.
2181 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2182 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2183 // Unless we are bitcasing to the original type, disallow optimizations.
2184 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002185
2186 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2187 [secondOp-Instruction::CastOpsBegin];
2188 switch (ElimCase) {
2189 case 0:
2190 // categorically disallowed
2191 return 0;
2192 case 1:
2193 // allowed, use first cast's opcode
2194 return firstOp;
2195 case 2:
2196 // allowed, use second cast's opcode
2197 return secondOp;
2198 case 3:
2199 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002200 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002201 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002202 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002203 return firstOp;
2204 return 0;
2205 case 4:
2206 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002207 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002208 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002209 return firstOp;
2210 return 0;
2211 case 5:
2212 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002213 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002214 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002215 return secondOp;
2216 return 0;
2217 case 6:
2218 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002219 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002220 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002221 return secondOp;
2222 return 0;
2223 case 7: {
2224 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002225 if (!IntPtrTy)
2226 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002227 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2228 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002229 if (MidSize >= PtrSize)
2230 return Instruction::BitCast;
2231 return 0;
2232 }
2233 case 8: {
2234 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2235 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2236 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002237 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2238 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002239 if (SrcSize == DstSize)
2240 return Instruction::BitCast;
2241 else if (SrcSize < DstSize)
2242 return firstOp;
2243 return secondOp;
2244 }
2245 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2246 return Instruction::ZExt;
2247 case 10:
2248 // fpext followed by ftrunc is allowed if the bit size returned to is
2249 // the same as the original, in which case its just a bitcast
2250 if (SrcTy == DstTy)
2251 return Instruction::BitCast;
2252 return 0; // If the types are not the same we can't eliminate it.
2253 case 11:
2254 // bitcast followed by ptrtoint is allowed as long as the bitcast
2255 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002256 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002257 return secondOp;
2258 return 0;
2259 case 12:
2260 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002261 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002262 return firstOp;
2263 return 0;
2264 case 13: {
2265 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002266 if (!IntPtrTy)
2267 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002268 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2269 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2270 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271 if (SrcSize <= PtrSize && SrcSize == DstSize)
2272 return Instruction::BitCast;
2273 return 0;
2274 }
2275 case 99:
2276 // cast combination can't happen (error in input). This is for all cases
2277 // where the MidTy is not the same for the two cast instructions.
Richard Trieua318b8d2011-09-21 03:09:09 +00002278 assert(0 && "Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279 return 0;
2280 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002281 assert(0 && "Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282 return 0;
2283 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002284}
2285
Chris Lattner229907c2011-07-18 04:54:35 +00002286CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002287 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002288 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289 // Construct and return the appropriate CastInst subclass
2290 switch (op) {
2291 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2292 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2293 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2294 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2295 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2296 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2297 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2298 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2299 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2300 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2301 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2302 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2303 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002304 assert(0 && "Invalid opcode provided");
David Blaikie46a9f012012-01-20 21:51:11 +00002305 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307}
2308
Chris Lattner229907c2011-07-18 04:54:35 +00002309CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002310 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002311 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312 // Construct and return the appropriate CastInst subclass
2313 switch (op) {
2314 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2315 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2316 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2317 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2318 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2319 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2320 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2321 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2322 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2323 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2324 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2325 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2326 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002327 assert(0 && "Invalid opcode provided");
David Blaikie46a9f012012-01-20 21:51:11 +00002328 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330}
2331
Chris Lattner229907c2011-07-18 04:54:35 +00002332CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002333 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002334 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002335 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002336 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2337 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002338}
2339
Chris Lattner229907c2011-07-18 04:54:35 +00002340CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002341 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002342 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002343 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002344 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2345 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002346}
2347
Chris Lattner229907c2011-07-18 04:54:35 +00002348CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002349 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002350 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002351 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002352 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2353 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002354}
2355
Chris Lattner229907c2011-07-18 04:54:35 +00002356CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002357 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002358 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002359 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002360 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2361 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002362}
2363
Chris Lattner229907c2011-07-18 04:54:35 +00002364CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002365 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002366 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002367 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002368 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2369 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002370}
2371
Chris Lattner229907c2011-07-18 04:54:35 +00002372CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002373 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002374 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002375 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002376 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2377 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002378}
2379
Chris Lattner229907c2011-07-18 04:54:35 +00002380CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002381 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002382 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002383 assert(S->getType()->isPointerTy() && "Invalid cast");
2384 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002385 "Invalid cast");
2386
Duncan Sands9dff9be2010-02-15 16:12:20 +00002387 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002388 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2389 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002390}
2391
2392/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002393CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002394 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002395 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002396 assert(S->getType()->isPointerTy() && "Invalid cast");
2397 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002398 "Invalid cast");
2399
Duncan Sands9dff9be2010-02-15 16:12:20 +00002400 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002401 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2402 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002403}
2404
Chris Lattner229907c2011-07-18 04:54:35 +00002405CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002406 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002407 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002408 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002409 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002410 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2411 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002412 Instruction::CastOps opcode =
2413 (SrcBits == DstBits ? Instruction::BitCast :
2414 (SrcBits > DstBits ? Instruction::Trunc :
2415 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002416 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002417}
2418
Chris Lattner229907c2011-07-18 04:54:35 +00002419CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002420 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002421 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002422 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002423 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002424 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2425 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002426 Instruction::CastOps opcode =
2427 (SrcBits == DstBits ? Instruction::BitCast :
2428 (SrcBits > DstBits ? Instruction::Trunc :
2429 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002430 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002431}
2432
Chris Lattner229907c2011-07-18 04:54:35 +00002433CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002434 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002435 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002436 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002437 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002438 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2439 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002440 Instruction::CastOps opcode =
2441 (SrcBits == DstBits ? Instruction::BitCast :
2442 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002443 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002444}
2445
Chris Lattner229907c2011-07-18 04:54:35 +00002446CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002447 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002448 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002449 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002450 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002451 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2452 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002453 Instruction::CastOps opcode =
2454 (SrcBits == DstBits ? Instruction::BitCast :
2455 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002456 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002457}
2458
Duncan Sands55e50902008-01-06 10:12:28 +00002459// Check whether it is valid to call getCastOpcode for these types.
2460// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002461bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002462 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2463 return false;
2464
2465 if (SrcTy == DestTy)
2466 return true;
2467
Chris Lattner229907c2011-07-18 04:54:35 +00002468 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2469 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002470 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2471 // An element by element cast. Valid if casting the elements is valid.
2472 SrcTy = SrcVecTy->getElementType();
2473 DestTy = DestVecTy->getElementType();
2474 }
2475
Duncan Sands55e50902008-01-06 10:12:28 +00002476 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002477 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2478 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002479
2480 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002481 if (DestTy->isIntegerTy()) { // Casting to integral
2482 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002483 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002484 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002485 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002486 } else if (SrcTy->isVectorTy()) { // Casting from vector
2487 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002488 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002489 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002490 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002491 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2492 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002493 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002494 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002495 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002496 } else if (SrcTy->isVectorTy()) { // Casting from vector
2497 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002498 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002499 return false;
2500 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002501 } else if (DestTy->isVectorTy()) { // Casting to vector
2502 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002503 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002504 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002505 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002506 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002507 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002508 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002509 return false;
2510 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002511 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002512 if (SrcTy->isVectorTy()) {
2513 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002514 } else {
2515 return false;
2516 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002517 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002518 return false;
2519 }
2520}
2521
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522// Provide a way to get a "cast" where the cast opcode is inferred from the
2523// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002524// logic in the castIsValid function below. This axiom should hold:
2525// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2526// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002528// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002530CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002531 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2532 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533
Duncan Sands55e50902008-01-06 10:12:28 +00002534 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2535 "Only first class types are castable!");
2536
Duncan Sandsa8514532011-05-18 07:13:41 +00002537 if (SrcTy == DestTy)
2538 return BitCast;
2539
Chris Lattner229907c2011-07-18 04:54:35 +00002540 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2541 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002542 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2543 // An element by element cast. Find the appropriate opcode based on the
2544 // element types.
2545 SrcTy = SrcVecTy->getElementType();
2546 DestTy = DestVecTy->getElementType();
2547 }
2548
2549 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002550 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2551 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002552
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002554 if (DestTy->isIntegerTy()) { // Casting to integral
2555 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556 if (DestBits < SrcBits)
2557 return Trunc; // int -> smaller int
2558 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002559 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560 return SExt; // signed -> SEXT
2561 else
2562 return ZExt; // unsigned -> ZEXT
2563 } else {
2564 return BitCast; // Same size, No-op cast
2565 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002566 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002567 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568 return FPToSI; // FP -> sint
2569 else
2570 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002571 } else if (SrcTy->isVectorTy()) {
2572 assert(DestBits == SrcBits &&
2573 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574 return BitCast; // Same size, no-op cast
2575 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002576 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577 "Casting from a value that is not first-class type");
2578 return PtrToInt; // ptr -> int
2579 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002580 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2581 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002582 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583 return SIToFP; // sint -> FP
2584 else
2585 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002586 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587 if (DestBits < SrcBits) {
2588 return FPTrunc; // FP -> smaller FP
2589 } else if (DestBits > SrcBits) {
2590 return FPExt; // FP -> larger FP
2591 } else {
2592 return BitCast; // same size, no-op cast
2593 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002594 } else if (SrcTy->isVectorTy()) {
2595 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002596 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002597 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002599 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002601 } else if (DestTy->isVectorTy()) {
2602 assert(DestBits == SrcBits &&
2603 "Illegal cast to vector (wrong type or size)");
2604 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002605 } else if (DestTy->isPointerTy()) {
2606 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002608 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609 return IntToPtr; // int -> ptr
2610 } else {
Richard Trieua318b8d2011-09-21 03:09:09 +00002611 assert(0 && "Casting pointer to other than pointer or int");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002613 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002614 if (SrcTy->isVectorTy()) {
2615 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002616 return BitCast; // 64-bit vector to MMX
2617 } else {
Richard Trieua318b8d2011-09-21 03:09:09 +00002618 assert(0 && "Illegal cast to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002619 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620 } else {
Richard Trieua318b8d2011-09-21 03:09:09 +00002621 assert(0 && "Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622 }
2623
2624 // If we fall through to here we probably hit an assertion cast above
2625 // and assertions are not turned on. Anything we return is an error, so
2626 // BitCast is as good a choice as any.
2627 return BitCast;
2628}
2629
2630//===----------------------------------------------------------------------===//
2631// CastInst SubClass Constructors
2632//===----------------------------------------------------------------------===//
2633
2634/// Check that the construction parameters for a CastInst are correct. This
2635/// could be broken out into the separate constructors but it is useful to have
2636/// it in one place and to eliminate the redundant code for getting the sizes
2637/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002638bool
Chris Lattner229907c2011-07-18 04:54:35 +00002639CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640
2641 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002642 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002643 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2644 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645 return false;
2646
2647 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002648 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2649 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002650
Duncan Sands7f646562011-05-18 09:21:57 +00002651 // If these are vector types, get the lengths of the vectors (using zero for
2652 // scalar types means that checking that vector lengths match also checks that
2653 // scalars are not being converted to vectors or vectors to scalars).
2654 unsigned SrcLength = SrcTy->isVectorTy() ?
2655 cast<VectorType>(SrcTy)->getNumElements() : 0;
2656 unsigned DstLength = DstTy->isVectorTy() ?
2657 cast<VectorType>(DstTy)->getNumElements() : 0;
2658
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659 // Switch on the opcode provided
2660 switch (op) {
2661 default: return false; // This is an input error
2662 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002663 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2664 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002666 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2667 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002668 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002669 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2670 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002672 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2673 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002675 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2676 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002678 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002679 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2680 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002683 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2684 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002685 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002686 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002687 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002688 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2689 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2690 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002691 return SrcTy->getScalarType()->isPointerTy() &&
2692 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002693 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002694 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002695 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002696 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2697 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2698 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002699 return SrcTy->getScalarType()->isIntegerTy() &&
2700 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002701 case Instruction::BitCast:
2702 // BitCast implies a no-op cast of type only. No bits change.
2703 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002704 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002705 return false;
2706
Duncan Sands55e50902008-01-06 10:12:28 +00002707 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002708 // these cases, the cast is okay if the source and destination bit widths
2709 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002710 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002711 }
2712}
2713
2714TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002715 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002716) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002717 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002718}
2719
2720TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002721 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002722) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002723 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002724}
2725
2726ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002727 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002729 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002730}
2731
2732ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002733 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002734) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002735 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002736}
2737SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002738 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002740 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002741}
2742
Jeff Cohencc08c832006-12-02 02:22:01 +00002743SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002744 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002745) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002746 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002747}
2748
2749FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002750 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002751) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002752 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002753}
2754
2755FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002756 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002757) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002758 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002759}
2760
2761FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002762 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002763) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002764 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002765}
2766
2767FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002768 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002769) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002770 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771}
2772
2773UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002774 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002775) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002776 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002777}
2778
2779UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002780 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002781) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002782 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002783}
2784
2785SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002786 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002787) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002788 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002789}
2790
2791SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002792 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002793) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002794 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002795}
2796
2797FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002798 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002799) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002800 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002801}
2802
2803FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002804 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002805) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002806 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002807}
2808
2809FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002810 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002811) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002812 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002813}
2814
2815FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002816 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002817) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002818 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002819}
2820
2821PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002822 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002823) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002824 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002825}
2826
2827PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002828 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002829) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002830 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002831}
2832
2833IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002834 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002835) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002836 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002837}
2838
2839IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002840 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002841) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002842 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002843}
2844
2845BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002846 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002847) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002848 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002849}
2850
2851BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002852 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002853) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002854 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002855}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002856
2857//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002858// CmpInst Classes
2859//===----------------------------------------------------------------------===//
2860
Chris Lattneraec33da2010-01-22 06:25:37 +00002861void CmpInst::Anchor() const {}
2862
Chris Lattner229907c2011-07-18 04:54:35 +00002863CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002864 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002865 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002866 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002867 OperandTraits<CmpInst>::op_begin(this),
2868 OperandTraits<CmpInst>::operands(this),
2869 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002870 Op<0>() = LHS;
2871 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002872 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002873 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002874}
Gabor Greiff6caff662008-05-10 08:32:32 +00002875
Chris Lattner229907c2011-07-18 04:54:35 +00002876CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002877 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002878 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002879 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002880 OperandTraits<CmpInst>::op_begin(this),
2881 OperandTraits<CmpInst>::operands(this),
2882 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002883 Op<0>() = LHS;
2884 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002885 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002886 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002887}
2888
2889CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002890CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002891 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002892 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002893 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002894 if (InsertBefore)
2895 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2896 S1, S2, Name);
2897 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002898 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002899 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002900 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002901
2902 if (InsertBefore)
2903 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2904 S1, S2, Name);
2905 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002906 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002907 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002908}
2909
2910CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002911CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002912 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002913 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002914 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2915 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002916 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002917 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2918 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002919}
2920
2921void CmpInst::swapOperands() {
2922 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2923 IC->swapOperands();
2924 else
2925 cast<FCmpInst>(this)->swapOperands();
2926}
2927
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002928bool CmpInst::isCommutative() const {
2929 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002930 return IC->isCommutative();
2931 return cast<FCmpInst>(this)->isCommutative();
2932}
2933
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002934bool CmpInst::isEquality() const {
2935 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002936 return IC->isEquality();
2937 return cast<FCmpInst>(this)->isEquality();
2938}
2939
2940
Dan Gohman4e724382008-05-31 02:47:54 +00002941CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002942 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002943 default: assert(0 && "Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002944 case ICMP_EQ: return ICMP_NE;
2945 case ICMP_NE: return ICMP_EQ;
2946 case ICMP_UGT: return ICMP_ULE;
2947 case ICMP_ULT: return ICMP_UGE;
2948 case ICMP_UGE: return ICMP_ULT;
2949 case ICMP_ULE: return ICMP_UGT;
2950 case ICMP_SGT: return ICMP_SLE;
2951 case ICMP_SLT: return ICMP_SGE;
2952 case ICMP_SGE: return ICMP_SLT;
2953 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002954
Dan Gohman4e724382008-05-31 02:47:54 +00002955 case FCMP_OEQ: return FCMP_UNE;
2956 case FCMP_ONE: return FCMP_UEQ;
2957 case FCMP_OGT: return FCMP_ULE;
2958 case FCMP_OLT: return FCMP_UGE;
2959 case FCMP_OGE: return FCMP_ULT;
2960 case FCMP_OLE: return FCMP_UGT;
2961 case FCMP_UEQ: return FCMP_ONE;
2962 case FCMP_UNE: return FCMP_OEQ;
2963 case FCMP_UGT: return FCMP_OLE;
2964 case FCMP_ULT: return FCMP_OGE;
2965 case FCMP_UGE: return FCMP_OLT;
2966 case FCMP_ULE: return FCMP_OGT;
2967 case FCMP_ORD: return FCMP_UNO;
2968 case FCMP_UNO: return FCMP_ORD;
2969 case FCMP_TRUE: return FCMP_FALSE;
2970 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002971 }
2972}
2973
Reid Spencer266e42b2006-12-23 06:05:41 +00002974ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2975 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002976 default: assert(0 && "Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002977 case ICMP_EQ: case ICMP_NE:
2978 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2979 return pred;
2980 case ICMP_UGT: return ICMP_SGT;
2981 case ICMP_ULT: return ICMP_SLT;
2982 case ICMP_UGE: return ICMP_SGE;
2983 case ICMP_ULE: return ICMP_SLE;
2984 }
2985}
2986
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002987ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2988 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002989 default: assert(0 && "Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002990 case ICMP_EQ: case ICMP_NE:
2991 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2992 return pred;
2993 case ICMP_SGT: return ICMP_UGT;
2994 case ICMP_SLT: return ICMP_ULT;
2995 case ICMP_SGE: return ICMP_UGE;
2996 case ICMP_SLE: return ICMP_ULE;
2997 }
2998}
2999
Reid Spencer0286bc12007-02-28 22:00:54 +00003000/// Initialize a set of values that all satisfy the condition with C.
3001///
3002ConstantRange
3003ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3004 APInt Lower(C);
3005 APInt Upper(C);
3006 uint32_t BitWidth = C.getBitWidth();
3007 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003008 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00003009 case ICmpInst::ICMP_EQ: Upper++; break;
3010 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003011 case ICmpInst::ICMP_ULT:
3012 Lower = APInt::getMinValue(BitWidth);
3013 // Check for an empty-set condition.
3014 if (Lower == Upper)
3015 return ConstantRange(BitWidth, /*isFullSet=*/false);
3016 break;
3017 case ICmpInst::ICMP_SLT:
3018 Lower = APInt::getSignedMinValue(BitWidth);
3019 // Check for an empty-set condition.
3020 if (Lower == Upper)
3021 return ConstantRange(BitWidth, /*isFullSet=*/false);
3022 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003023 case ICmpInst::ICMP_UGT:
3024 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003025 // Check for an empty-set condition.
3026 if (Lower == Upper)
3027 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003028 break;
3029 case ICmpInst::ICMP_SGT:
3030 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003031 // Check for an empty-set condition.
3032 if (Lower == Upper)
3033 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003034 break;
3035 case ICmpInst::ICMP_ULE:
3036 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003037 // Check for a full-set condition.
3038 if (Lower == Upper)
3039 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003040 break;
3041 case ICmpInst::ICMP_SLE:
3042 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003043 // Check for a full-set condition.
3044 if (Lower == Upper)
3045 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003046 break;
3047 case ICmpInst::ICMP_UGE:
3048 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003049 // Check for a full-set condition.
3050 if (Lower == Upper)
3051 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003052 break;
3053 case ICmpInst::ICMP_SGE:
3054 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003055 // Check for a full-set condition.
3056 if (Lower == Upper)
3057 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003058 break;
3059 }
3060 return ConstantRange(Lower, Upper);
3061}
3062
Dan Gohman4e724382008-05-31 02:47:54 +00003063CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003064 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00003065 default: assert(0 && "Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003066 case ICMP_EQ: case ICMP_NE:
3067 return pred;
3068 case ICMP_SGT: return ICMP_SLT;
3069 case ICMP_SLT: return ICMP_SGT;
3070 case ICMP_SGE: return ICMP_SLE;
3071 case ICMP_SLE: return ICMP_SGE;
3072 case ICMP_UGT: return ICMP_ULT;
3073 case ICMP_ULT: return ICMP_UGT;
3074 case ICMP_UGE: return ICMP_ULE;
3075 case ICMP_ULE: return ICMP_UGE;
3076
Reid Spencerd9436b62006-11-20 01:22:35 +00003077 case FCMP_FALSE: case FCMP_TRUE:
3078 case FCMP_OEQ: case FCMP_ONE:
3079 case FCMP_UEQ: case FCMP_UNE:
3080 case FCMP_ORD: case FCMP_UNO:
3081 return pred;
3082 case FCMP_OGT: return FCMP_OLT;
3083 case FCMP_OLT: return FCMP_OGT;
3084 case FCMP_OGE: return FCMP_OLE;
3085 case FCMP_OLE: return FCMP_OGE;
3086 case FCMP_UGT: return FCMP_ULT;
3087 case FCMP_ULT: return FCMP_UGT;
3088 case FCMP_UGE: return FCMP_ULE;
3089 case FCMP_ULE: return FCMP_UGE;
3090 }
3091}
3092
Reid Spencer266e42b2006-12-23 06:05:41 +00003093bool CmpInst::isUnsigned(unsigned short predicate) {
3094 switch (predicate) {
3095 default: return false;
3096 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3097 case ICmpInst::ICMP_UGE: return true;
3098 }
3099}
3100
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003101bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003102 switch (predicate) {
3103 default: return false;
3104 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3105 case ICmpInst::ICMP_SGE: return true;
3106 }
3107}
3108
3109bool CmpInst::isOrdered(unsigned short predicate) {
3110 switch (predicate) {
3111 default: return false;
3112 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3113 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3114 case FCmpInst::FCMP_ORD: return true;
3115 }
3116}
3117
3118bool CmpInst::isUnordered(unsigned short predicate) {
3119 switch (predicate) {
3120 default: return false;
3121 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3122 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3123 case FCmpInst::FCMP_UNO: return true;
3124 }
3125}
3126
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003127bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3128 switch(predicate) {
3129 default: return false;
3130 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3131 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3132 }
3133}
3134
3135bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3136 switch(predicate) {
3137 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3138 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3139 default: return false;
3140 }
3141}
3142
3143
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003144//===----------------------------------------------------------------------===//
3145// SwitchInst Implementation
3146//===----------------------------------------------------------------------===//
3147
Chris Lattnerbaf00152010-11-17 05:41:46 +00003148void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3149 assert(Value && Default && NumReserved);
3150 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003151 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003152 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003153
Gabor Greif2d3024d2008-05-26 21:33:52 +00003154 OperandList[0] = Value;
3155 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003156}
3157
Chris Lattner2195fc42007-02-24 00:55:48 +00003158/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3159/// switch on and a default destination. The number of additional cases can
3160/// be specified here to make memory allocation more efficient. This
3161/// constructor can also autoinsert before another instruction.
3162SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3163 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003164 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3165 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003166 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003167}
3168
3169/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3170/// switch on and a default destination. The number of additional cases can
3171/// be specified here to make memory allocation more efficient. This
3172/// constructor also autoinserts at the end of the specified BasicBlock.
3173SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3174 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003175 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3176 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003177 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003178}
3179
Misha Brukmanb1c93172005-04-21 23:48:37 +00003180SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003181 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3182 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3183 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003184 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003185 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003186 OL[i] = InOL[i];
3187 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003188 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003189 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003190}
3191
Gordon Henriksen14a55692007-12-10 02:14:30 +00003192SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003193 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003194}
3195
3196
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003197/// addCase - Add an entry to the switch instruction...
3198///
Chris Lattner47ac1872005-02-24 05:32:09 +00003199void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003200 unsigned OpNo = NumOperands;
3201 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003202 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003203 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003204 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003205 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003206 OperandList[OpNo] = OnVal;
3207 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003208}
3209
3210/// removeCase - This method removes the specified successor from the switch
3211/// instruction. Note that this cannot be used to remove the default
3212/// destination (successor #0).
3213///
3214void SwitchInst::removeCase(unsigned idx) {
3215 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003216 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3217
3218 unsigned NumOps = getNumOperands();
3219 Use *OL = OperandList;
3220
Jay Foad14277722011-02-01 09:22:34 +00003221 // Overwrite this case with the end of the list.
3222 if ((idx + 1) * 2 != NumOps) {
3223 OL[idx * 2] = OL[NumOps - 2];
3224 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003225 }
3226
3227 // Nuke the last value.
3228 OL[NumOps-2].set(0);
3229 OL[NumOps-2+1].set(0);
3230 NumOperands = NumOps-2;
3231}
3232
Jay Foade98f29d2011-04-01 08:00:58 +00003233/// growOperands - grow operands - This grows the operand list in response
3234/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003235///
Jay Foade98f29d2011-04-01 08:00:58 +00003236void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003237 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003238 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003239
3240 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003241 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003242 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003243 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003244 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003245 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003246 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003247 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003248}
3249
3250
3251BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3252 return getSuccessor(idx);
3253}
3254unsigned SwitchInst::getNumSuccessorsV() const {
3255 return getNumSuccessors();
3256}
3257void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3258 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003259}
Chris Lattnerf22be932004-10-15 23:52:53 +00003260
Chris Lattner3ed871f2009-10-27 19:13:16 +00003261//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003262// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003263//===----------------------------------------------------------------------===//
3264
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003265void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003266 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003267 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003268 ReservedSpace = 1+NumDests;
3269 NumOperands = 1;
3270 OperandList = allocHungoffUses(ReservedSpace);
3271
3272 OperandList[0] = Address;
3273}
3274
3275
Jay Foade98f29d2011-04-01 08:00:58 +00003276/// growOperands - grow operands - This grows the operand list in response
3277/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003278///
Jay Foade98f29d2011-04-01 08:00:58 +00003279void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003280 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003281 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003282
3283 ReservedSpace = NumOps;
3284 Use *NewOps = allocHungoffUses(NumOps);
3285 Use *OldOps = OperandList;
3286 for (unsigned i = 0; i != e; ++i)
3287 NewOps[i] = OldOps[i];
3288 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003289 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003290}
3291
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003292IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3293 Instruction *InsertBefore)
3294: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003295 0, 0, InsertBefore) {
3296 init(Address, NumCases);
3297}
3298
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003299IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3300 BasicBlock *InsertAtEnd)
3301: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003302 0, 0, InsertAtEnd) {
3303 init(Address, NumCases);
3304}
3305
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003306IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3307 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003308 allocHungoffUses(IBI.getNumOperands()),
3309 IBI.getNumOperands()) {
3310 Use *OL = OperandList, *InOL = IBI.OperandList;
3311 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3312 OL[i] = InOL[i];
3313 SubclassOptionalData = IBI.SubclassOptionalData;
3314}
3315
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003316IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003317 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003318}
3319
3320/// addDestination - Add a destination.
3321///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003322void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003323 unsigned OpNo = NumOperands;
3324 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003325 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003326 // Initialize some new operands.
3327 assert(OpNo < ReservedSpace && "Growing didn't work!");
3328 NumOperands = OpNo+1;
3329 OperandList[OpNo] = DestBB;
3330}
3331
3332/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003333/// indirectbr instruction.
3334void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003335 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3336
3337 unsigned NumOps = getNumOperands();
3338 Use *OL = OperandList;
3339
3340 // Replace this value with the last one.
3341 OL[idx+1] = OL[NumOps-1];
3342
3343 // Nuke the last value.
3344 OL[NumOps-1].set(0);
3345 NumOperands = NumOps-1;
3346}
3347
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003348BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003349 return getSuccessor(idx);
3350}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003351unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003352 return getNumSuccessors();
3353}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003354void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003355 setSuccessor(idx, B);
3356}
3357
3358//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003359// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003360//===----------------------------------------------------------------------===//
3361
Chris Lattnerf22be932004-10-15 23:52:53 +00003362// Define these methods here so vtables don't get emitted into every translation
3363// unit that uses these classes.
3364
Devang Patel11cf3f42009-10-27 22:16:29 +00003365GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3366 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003367}
3368
Devang Patel11cf3f42009-10-27 22:16:29 +00003369BinaryOperator *BinaryOperator::clone_impl() const {
3370 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003371}
3372
Devang Patel11cf3f42009-10-27 22:16:29 +00003373FCmpInst* FCmpInst::clone_impl() const {
3374 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003375}
3376
Devang Patel11cf3f42009-10-27 22:16:29 +00003377ICmpInst* ICmpInst::clone_impl() const {
3378 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003379}
3380
Devang Patel11cf3f42009-10-27 22:16:29 +00003381ExtractValueInst *ExtractValueInst::clone_impl() const {
3382 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003383}
3384
Devang Patel11cf3f42009-10-27 22:16:29 +00003385InsertValueInst *InsertValueInst::clone_impl() const {
3386 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003387}
3388
Devang Patel11cf3f42009-10-27 22:16:29 +00003389AllocaInst *AllocaInst::clone_impl() const {
3390 return new AllocaInst(getAllocatedType(),
3391 (Value*)getOperand(0),
3392 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003393}
3394
Devang Patel11cf3f42009-10-27 22:16:29 +00003395LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003396 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3397 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003398}
3399
Devang Patel11cf3f42009-10-27 22:16:29 +00003400StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003401 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003402 getAlignment(), getOrdering(), getSynchScope());
3403
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003404}
3405
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003406AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3407 AtomicCmpXchgInst *Result =
3408 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3409 getOrdering(), getSynchScope());
3410 Result->setVolatile(isVolatile());
3411 return Result;
3412}
3413
3414AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3415 AtomicRMWInst *Result =
3416 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3417 getOrdering(), getSynchScope());
3418 Result->setVolatile(isVolatile());
3419 return Result;
3420}
3421
Eli Friedmanfee02c62011-07-25 23:16:38 +00003422FenceInst *FenceInst::clone_impl() const {
3423 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3424}
3425
Devang Patel11cf3f42009-10-27 22:16:29 +00003426TruncInst *TruncInst::clone_impl() const {
3427 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003428}
3429
Devang Patel11cf3f42009-10-27 22:16:29 +00003430ZExtInst *ZExtInst::clone_impl() const {
3431 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003432}
3433
Devang Patel11cf3f42009-10-27 22:16:29 +00003434SExtInst *SExtInst::clone_impl() const {
3435 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003436}
3437
Devang Patel11cf3f42009-10-27 22:16:29 +00003438FPTruncInst *FPTruncInst::clone_impl() const {
3439 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003440}
3441
Devang Patel11cf3f42009-10-27 22:16:29 +00003442FPExtInst *FPExtInst::clone_impl() const {
3443 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003444}
3445
Devang Patel11cf3f42009-10-27 22:16:29 +00003446UIToFPInst *UIToFPInst::clone_impl() const {
3447 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003448}
3449
Devang Patel11cf3f42009-10-27 22:16:29 +00003450SIToFPInst *SIToFPInst::clone_impl() const {
3451 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003452}
3453
Devang Patel11cf3f42009-10-27 22:16:29 +00003454FPToUIInst *FPToUIInst::clone_impl() const {
3455 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003456}
3457
Devang Patel11cf3f42009-10-27 22:16:29 +00003458FPToSIInst *FPToSIInst::clone_impl() const {
3459 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003460}
3461
Devang Patel11cf3f42009-10-27 22:16:29 +00003462PtrToIntInst *PtrToIntInst::clone_impl() const {
3463 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003464}
3465
Devang Patel11cf3f42009-10-27 22:16:29 +00003466IntToPtrInst *IntToPtrInst::clone_impl() const {
3467 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003468}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003469
Devang Patel11cf3f42009-10-27 22:16:29 +00003470BitCastInst *BitCastInst::clone_impl() const {
3471 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003472}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003473
Devang Patel11cf3f42009-10-27 22:16:29 +00003474CallInst *CallInst::clone_impl() const {
3475 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003476}
3477
Devang Patel11cf3f42009-10-27 22:16:29 +00003478SelectInst *SelectInst::clone_impl() const {
3479 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003480}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003481
Devang Patel11cf3f42009-10-27 22:16:29 +00003482VAArgInst *VAArgInst::clone_impl() const {
3483 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003484}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003485
Devang Patel11cf3f42009-10-27 22:16:29 +00003486ExtractElementInst *ExtractElementInst::clone_impl() const {
3487 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003488}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003489
Devang Patel11cf3f42009-10-27 22:16:29 +00003490InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003491 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003492}
3493
Devang Patel11cf3f42009-10-27 22:16:29 +00003494ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003495 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003496}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003497
Devang Patel11cf3f42009-10-27 22:16:29 +00003498PHINode *PHINode::clone_impl() const {
3499 return new PHINode(*this);
3500}
3501
Bill Wendlingfae14752011-08-12 20:24:12 +00003502LandingPadInst *LandingPadInst::clone_impl() const {
3503 return new LandingPadInst(*this);
3504}
3505
Devang Patel11cf3f42009-10-27 22:16:29 +00003506ReturnInst *ReturnInst::clone_impl() const {
3507 return new(getNumOperands()) ReturnInst(*this);
3508}
3509
3510BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003511 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003512}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003513
Devang Patel11cf3f42009-10-27 22:16:29 +00003514SwitchInst *SwitchInst::clone_impl() const {
3515 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003516}
3517
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003518IndirectBrInst *IndirectBrInst::clone_impl() const {
3519 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003520}
3521
3522
Devang Patel11cf3f42009-10-27 22:16:29 +00003523InvokeInst *InvokeInst::clone_impl() const {
3524 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003525}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003526
Bill Wendlingf891bf82011-07-31 06:30:59 +00003527ResumeInst *ResumeInst::clone_impl() const {
3528 return new(1) ResumeInst(*this);
3529}
3530
Devang Patel11cf3f42009-10-27 22:16:29 +00003531UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003532 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003533 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003534}
3535
Devang Patel11cf3f42009-10-27 22:16:29 +00003536UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003537 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003538 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003539}