blob: 185cd0557c17e4f967aeeae7922b1d160a571173 [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//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000634// ResumeInst Implementation
635//===----------------------------------------------------------------------===//
636
637ResumeInst::ResumeInst(const ResumeInst &RI)
638 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
639 OperandTraits<ResumeInst>::op_begin(this), 1) {
640 Op<0>() = RI.Op<0>();
641}
642
643ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
644 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
645 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
646 Op<0>() = Exn;
647}
648
649ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
650 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
651 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
652 Op<0>() = Exn;
653}
654
655unsigned ResumeInst::getNumSuccessorsV() const {
656 return getNumSuccessors();
657}
658
659void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
660 llvm_unreachable("ResumeInst has no successors!");
661}
662
663BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
664 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000665}
666
667//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000668// UnreachableInst Implementation
669//===----------------------------------------------------------------------===//
670
Owen Anderson55f1c092009-08-13 21:58:54 +0000671UnreachableInst::UnreachableInst(LLVMContext &Context,
672 Instruction *InsertBefore)
673 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
674 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000675}
Owen Anderson55f1c092009-08-13 21:58:54 +0000676UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
677 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
678 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000679}
680
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000681unsigned UnreachableInst::getNumSuccessorsV() const {
682 return getNumSuccessors();
683}
684
685void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000686 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687}
688
689BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000690 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000691}
692
693//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000694// BranchInst Implementation
695//===----------------------------------------------------------------------===//
696
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000697void BranchInst::AssertOK() {
698 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000699 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000700 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000701}
702
Chris Lattner2195fc42007-02-24 00:55:48 +0000703BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000704 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000705 OperandTraits<BranchInst>::op_end(this) - 1,
706 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000707 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000708 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000709}
710BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
711 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000712 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000713 OperandTraits<BranchInst>::op_end(this) - 3,
714 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000715 Op<-1>() = IfTrue;
716 Op<-2>() = IfFalse;
717 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000718#ifndef NDEBUG
719 AssertOK();
720#endif
721}
722
723BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000724 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000725 OperandTraits<BranchInst>::op_end(this) - 1,
726 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000727 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000728 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000729}
730
731BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
732 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000733 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000734 OperandTraits<BranchInst>::op_end(this) - 3,
735 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000736 Op<-1>() = IfTrue;
737 Op<-2>() = IfFalse;
738 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000739#ifndef NDEBUG
740 AssertOK();
741#endif
742}
743
744
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000745BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000746 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000747 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
748 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000749 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000750 if (BI.getNumOperands() != 1) {
751 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000752 Op<-3>() = BI.Op<-3>();
753 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000754 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000755 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000756}
757
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000758void BranchInst::swapSuccessors() {
759 assert(isConditional() &&
760 "Cannot swap successors of an unconditional branch");
761 Op<-1>().swap(Op<-2>());
762
763 // Update profile metadata if present and it matches our structural
764 // expectations.
765 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
766 if (!ProfileData || ProfileData->getNumOperands() != 3)
767 return;
768
769 // The first operand is the name. Fetch them backwards and build a new one.
770 Value *Ops[] = {
771 ProfileData->getOperand(0),
772 ProfileData->getOperand(2),
773 ProfileData->getOperand(1)
774 };
775 setMetadata(LLVMContext::MD_prof,
776 MDNode::get(ProfileData->getContext(), Ops));
777}
778
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
780 return getSuccessor(idx);
781}
782unsigned BranchInst::getNumSuccessorsV() const {
783 return getNumSuccessors();
784}
785void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
786 setSuccessor(idx, B);
787}
788
789
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000791// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000792//===----------------------------------------------------------------------===//
793
Owen Andersonb6b25302009-07-14 23:09:55 +0000794static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000795 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000796 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000797 else {
798 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000799 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000800 assert(Amt->getType()->isIntegerTy() &&
801 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000802 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000803 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000804}
805
Chris Lattner229907c2011-07-18 04:54:35 +0000806AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000807 const Twine &Name, Instruction *InsertBefore)
808 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
809 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
810 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000811 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000812 setName(Name);
813}
814
Chris Lattner229907c2011-07-18 04:54:35 +0000815AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000816 const Twine &Name, BasicBlock *InsertAtEnd)
817 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
818 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
819 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000820 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000821 setName(Name);
822}
823
Chris Lattner229907c2011-07-18 04:54:35 +0000824AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000825 Instruction *InsertBefore)
826 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
827 getAISize(Ty->getContext(), 0), InsertBefore) {
828 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000829 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000830 setName(Name);
831}
832
Chris Lattner229907c2011-07-18 04:54:35 +0000833AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000834 BasicBlock *InsertAtEnd)
835 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
836 getAISize(Ty->getContext(), 0), InsertAtEnd) {
837 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000838 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000839 setName(Name);
840}
841
Chris Lattner229907c2011-07-18 04:54:35 +0000842AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000843 const Twine &Name, Instruction *InsertBefore)
844 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000845 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000846 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000847 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000848 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000849}
850
Chris Lattner229907c2011-07-18 04:54:35 +0000851AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000852 const Twine &Name, BasicBlock *InsertAtEnd)
853 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000854 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000855 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000856 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000857 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000858}
859
Gordon Henriksen14a55692007-12-10 02:14:30 +0000860// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000861AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000862}
863
Victor Hernandez8acf2952009-10-23 21:09:37 +0000864void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000865 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000866 assert(Align <= MaximumAlignment &&
867 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000868 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000869 assert(getAlignment() == Align && "Alignment representation error!");
870}
871
Victor Hernandez8acf2952009-10-23 21:09:37 +0000872bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000873 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000874 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000875 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000876}
877
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000878Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879 return getType()->getElementType();
880}
881
Chris Lattner8b291e62008-11-26 02:54:17 +0000882/// isStaticAlloca - Return true if this alloca is in the entry block of the
883/// function and is a constant size. If so, the code generator will fold it
884/// into the prolog/epilog code, so it is basically free.
885bool AllocaInst::isStaticAlloca() const {
886 // Must be constant size.
887 if (!isa<ConstantInt>(getArraySize())) return false;
888
889 // Must be in the entry block.
890 const BasicBlock *Parent = getParent();
891 return Parent == &Parent->getParent()->front();
892}
893
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000894//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000895// LoadInst Implementation
896//===----------------------------------------------------------------------===//
897
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000898void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000899 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000901 assert(!(isAtomic() && getAlignment() == 0) &&
902 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903}
904
Daniel Dunbar4975db62009-07-25 04:41:11 +0000905LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000906 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000907 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000908 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000909 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000910 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000911 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000912 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000913}
914
Daniel Dunbar4975db62009-07-25 04:41:11 +0000915LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000917 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000918 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000919 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000920 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000921 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000922 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923}
924
Daniel Dunbar4975db62009-07-25 04:41:11 +0000925LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000927 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000928 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000929 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000930 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000931 setAtomic(NotAtomic);
932 AssertOK();
933 setName(Name);
934}
935
936LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
937 BasicBlock *InsertAE)
938 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
939 Load, Ptr, InsertAE) {
940 setVolatile(isVolatile);
941 setAlignment(0);
942 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +0000943 AssertOK();
944 setName(Name);
945}
946
Daniel Dunbar4975db62009-07-25 04:41:11 +0000947LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000948 unsigned Align, Instruction *InsertBef)
949 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
950 Load, Ptr, InsertBef) {
951 setVolatile(isVolatile);
952 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000953 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000954 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000955 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000956}
957
Daniel Dunbar4975db62009-07-25 04:41:11 +0000958LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000959 unsigned Align, BasicBlock *InsertAE)
960 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
961 Load, Ptr, InsertAE) {
962 setVolatile(isVolatile);
963 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000964 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +0000965 AssertOK();
966 setName(Name);
967}
968
Eli Friedman59b66882011-08-09 23:02:53 +0000969LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
970 unsigned Align, AtomicOrdering Order,
971 SynchronizationScope SynchScope,
972 Instruction *InsertBef)
973 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
974 Load, Ptr, InsertBef) {
975 setVolatile(isVolatile);
976 setAlignment(Align);
977 setAtomic(Order, SynchScope);
978 AssertOK();
979 setName(Name);
980}
981
982LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
983 unsigned Align, AtomicOrdering Order,
984 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000985 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000986 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000987 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000988 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +0000989 setAlignment(Align);
990 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +0000991 AssertOK();
992 setName(Name);
993}
994
Daniel Dunbar27096822009-08-11 18:11:15 +0000995LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
996 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
997 Load, Ptr, InsertBef) {
998 setVolatile(false);
999 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001000 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001001 AssertOK();
1002 if (Name && Name[0]) setName(Name);
1003}
1004
1005LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1006 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1007 Load, Ptr, InsertAE) {
1008 setVolatile(false);
1009 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001010 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001011 AssertOK();
1012 if (Name && Name[0]) setName(Name);
1013}
1014
1015LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1016 Instruction *InsertBef)
1017: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1018 Load, Ptr, InsertBef) {
1019 setVolatile(isVolatile);
1020 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001021 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001022 AssertOK();
1023 if (Name && Name[0]) setName(Name);
1024}
1025
1026LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1027 BasicBlock *InsertAE)
1028 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1029 Load, Ptr, InsertAE) {
1030 setVolatile(isVolatile);
1031 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001032 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001033 AssertOK();
1034 if (Name && Name[0]) setName(Name);
1035}
1036
Christopher Lamb84485702007-04-22 19:24:39 +00001037void LoadInst::setAlignment(unsigned Align) {
1038 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001039 assert(Align <= MaximumAlignment &&
1040 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001041 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001042 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001043 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001044}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001045
1046//===----------------------------------------------------------------------===//
1047// StoreInst Implementation
1048//===----------------------------------------------------------------------===//
1049
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001050void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001051 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001052 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001053 "Ptr must have pointer type!");
1054 assert(getOperand(0)->getType() ==
1055 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001056 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001057 assert(!(isAtomic() && getAlignment() == 0) &&
1058 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001059}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001060
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001061
1062StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001063 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001064 OperandTraits<StoreInst>::op_begin(this),
1065 OperandTraits<StoreInst>::operands(this),
1066 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001067 Op<0>() = val;
1068 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001069 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001070 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001071 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001072 AssertOK();
1073}
1074
1075StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001076 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001077 OperandTraits<StoreInst>::op_begin(this),
1078 OperandTraits<StoreInst>::operands(this),
1079 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001080 Op<0>() = val;
1081 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001082 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001083 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001084 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001085 AssertOK();
1086}
1087
Misha Brukmanb1c93172005-04-21 23:48:37 +00001088StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001089 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001090 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001091 OperandTraits<StoreInst>::op_begin(this),
1092 OperandTraits<StoreInst>::operands(this),
1093 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001094 Op<0>() = val;
1095 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001096 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001097 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001098 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001099 AssertOK();
1100}
1101
1102StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1103 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001104 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001105 OperandTraits<StoreInst>::op_begin(this),
1106 OperandTraits<StoreInst>::operands(this),
1107 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001108 Op<0>() = val;
1109 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001110 setVolatile(isVolatile);
1111 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001112 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001113 AssertOK();
1114}
1115
Misha Brukmanb1c93172005-04-21 23:48:37 +00001116StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001117 unsigned Align, AtomicOrdering Order,
1118 SynchronizationScope SynchScope,
1119 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001120 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001121 OperandTraits<StoreInst>::op_begin(this),
1122 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001123 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001124 Op<0>() = val;
1125 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001126 setVolatile(isVolatile);
1127 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001128 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001129 AssertOK();
1130}
1131
1132StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001133 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001134 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001135 OperandTraits<StoreInst>::op_begin(this),
1136 OperandTraits<StoreInst>::operands(this),
1137 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001138 Op<0>() = val;
1139 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001140 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001141 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001142 setAtomic(NotAtomic);
1143 AssertOK();
1144}
1145
1146StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1147 unsigned Align, BasicBlock *InsertAtEnd)
1148 : Instruction(Type::getVoidTy(val->getContext()), Store,
1149 OperandTraits<StoreInst>::op_begin(this),
1150 OperandTraits<StoreInst>::operands(this),
1151 InsertAtEnd) {
1152 Op<0>() = val;
1153 Op<1>() = addr;
1154 setVolatile(isVolatile);
1155 setAlignment(Align);
1156 setAtomic(NotAtomic);
1157 AssertOK();
1158}
1159
1160StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1161 unsigned Align, AtomicOrdering Order,
1162 SynchronizationScope SynchScope,
1163 BasicBlock *InsertAtEnd)
1164 : Instruction(Type::getVoidTy(val->getContext()), Store,
1165 OperandTraits<StoreInst>::op_begin(this),
1166 OperandTraits<StoreInst>::operands(this),
1167 InsertAtEnd) {
1168 Op<0>() = val;
1169 Op<1>() = addr;
1170 setVolatile(isVolatile);
1171 setAlignment(Align);
1172 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001173 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001174}
1175
Christopher Lamb84485702007-04-22 19:24:39 +00001176void StoreInst::setAlignment(unsigned Align) {
1177 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001178 assert(Align <= MaximumAlignment &&
1179 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001180 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001181 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001182 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001183}
1184
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001185//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001186// AtomicCmpXchgInst Implementation
1187//===----------------------------------------------------------------------===//
1188
1189void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1190 AtomicOrdering Ordering,
1191 SynchronizationScope SynchScope) {
1192 Op<0>() = Ptr;
1193 Op<1>() = Cmp;
1194 Op<2>() = NewVal;
1195 setOrdering(Ordering);
1196 setSynchScope(SynchScope);
1197
1198 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1199 "All operands must be non-null!");
1200 assert(getOperand(0)->getType()->isPointerTy() &&
1201 "Ptr must have pointer type!");
1202 assert(getOperand(1)->getType() ==
1203 cast<PointerType>(getOperand(0)->getType())->getElementType()
1204 && "Ptr must be a pointer to Cmp type!");
1205 assert(getOperand(2)->getType() ==
1206 cast<PointerType>(getOperand(0)->getType())->getElementType()
1207 && "Ptr must be a pointer to NewVal type!");
1208 assert(Ordering != NotAtomic &&
1209 "AtomicCmpXchg instructions must be atomic!");
1210}
1211
1212AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1213 AtomicOrdering Ordering,
1214 SynchronizationScope SynchScope,
1215 Instruction *InsertBefore)
1216 : Instruction(Cmp->getType(), AtomicCmpXchg,
1217 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1218 OperandTraits<AtomicCmpXchgInst>::operands(this),
1219 InsertBefore) {
1220 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1221}
1222
1223AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1224 AtomicOrdering Ordering,
1225 SynchronizationScope SynchScope,
1226 BasicBlock *InsertAtEnd)
1227 : Instruction(Cmp->getType(), AtomicCmpXchg,
1228 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1229 OperandTraits<AtomicCmpXchgInst>::operands(this),
1230 InsertAtEnd) {
1231 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1232}
1233
1234//===----------------------------------------------------------------------===//
1235// AtomicRMWInst Implementation
1236//===----------------------------------------------------------------------===//
1237
1238void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1239 AtomicOrdering Ordering,
1240 SynchronizationScope SynchScope) {
1241 Op<0>() = Ptr;
1242 Op<1>() = Val;
1243 setOperation(Operation);
1244 setOrdering(Ordering);
1245 setSynchScope(SynchScope);
1246
1247 assert(getOperand(0) && getOperand(1) &&
1248 "All operands must be non-null!");
1249 assert(getOperand(0)->getType()->isPointerTy() &&
1250 "Ptr must have pointer type!");
1251 assert(getOperand(1)->getType() ==
1252 cast<PointerType>(getOperand(0)->getType())->getElementType()
1253 && "Ptr must be a pointer to Val type!");
1254 assert(Ordering != NotAtomic &&
1255 "AtomicRMW instructions must be atomic!");
1256}
1257
1258AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1259 AtomicOrdering Ordering,
1260 SynchronizationScope SynchScope,
1261 Instruction *InsertBefore)
1262 : Instruction(Val->getType(), AtomicRMW,
1263 OperandTraits<AtomicRMWInst>::op_begin(this),
1264 OperandTraits<AtomicRMWInst>::operands(this),
1265 InsertBefore) {
1266 Init(Operation, Ptr, Val, Ordering, SynchScope);
1267}
1268
1269AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1270 AtomicOrdering Ordering,
1271 SynchronizationScope SynchScope,
1272 BasicBlock *InsertAtEnd)
1273 : Instruction(Val->getType(), AtomicRMW,
1274 OperandTraits<AtomicRMWInst>::op_begin(this),
1275 OperandTraits<AtomicRMWInst>::operands(this),
1276 InsertAtEnd) {
1277 Init(Operation, Ptr, Val, Ordering, SynchScope);
1278}
1279
1280//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001281// FenceInst Implementation
1282//===----------------------------------------------------------------------===//
1283
1284FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1285 SynchronizationScope SynchScope,
1286 Instruction *InsertBefore)
1287 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1288 setOrdering(Ordering);
1289 setSynchScope(SynchScope);
1290}
1291
1292FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1293 SynchronizationScope SynchScope,
1294 BasicBlock *InsertAtEnd)
1295 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1296 setOrdering(Ordering);
1297 setSynchScope(SynchScope);
1298}
1299
1300//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001301// GetElementPtrInst Implementation
1302//===----------------------------------------------------------------------===//
1303
Jay Foadd1b78492011-07-25 09:48:08 +00001304void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001305 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001306 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1307 OperandList[0] = Ptr;
1308 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001309 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001310}
1311
Gabor Greiff6caff662008-05-10 08:32:32 +00001312GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001313 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001314 OperandTraits<GetElementPtrInst>::op_end(this)
1315 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001316 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001317 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001318 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001319}
1320
Chris Lattner6090a422009-03-09 04:46:40 +00001321/// getIndexedType - Returns the type of the element that would be accessed with
1322/// a gep instruction with the specified parameters.
1323///
1324/// The Idxs pointer should point to a continuous piece of memory containing the
1325/// indices, either as Value* or uint64_t.
1326///
1327/// A null type is returned if the indices are invalid for the specified
1328/// pointer type.
1329///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001330template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001331static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001332 if (Ptr->isVectorTy()) {
1333 assert(IdxList.size() == 1 &&
1334 "GEP with vector pointers must have a single index");
1335 PointerType *PTy = dyn_cast<PointerType>(
1336 cast<VectorType>(Ptr)->getElementType());
1337 assert(PTy && "Gep with invalid vector pointer found");
1338 return PTy->getElementType();
1339 }
1340
Chris Lattner229907c2011-07-18 04:54:35 +00001341 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001342 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001343 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001344
Chris Lattner6090a422009-03-09 04:46:40 +00001345 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001346 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001347 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001348
Chris Lattner6090a422009-03-09 04:46:40 +00001349 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001350 // it cannot be 'stepped over'.
1351 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001352 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001353
Dan Gohman1ecaf452008-05-31 00:58:22 +00001354 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001355 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001356 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001357 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001358 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001359 if (!CT->indexValid(Index)) return 0;
1360 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001361 }
Jay Foadd1b78492011-07-25 09:48:08 +00001362 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001363}
1364
Jay Foadd1b78492011-07-25 09:48:08 +00001365Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1366 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001367}
1368
Chris Lattner229907c2011-07-18 04:54:35 +00001369Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001370 ArrayRef<Constant *> IdxList) {
1371 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001372}
1373
Jay Foadd1b78492011-07-25 09:48:08 +00001374Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1375 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001376}
1377
Nadav Rotem3924cb02011-12-05 06:29:09 +00001378unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) {
1379 Type *Ty = Ptr->getType();
1380
1381 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1382 Ty = VTy->getElementType();
1383
1384 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
1385 return PTy->getAddressSpace();
1386
Craig Topperc514b542012-02-05 22:14:15 +00001387 llvm_unreachable("Invalid GEP pointer type");
Nadav Rotem3924cb02011-12-05 06:29:09 +00001388}
1389
Chris Lattner45f15572007-04-14 00:12:57 +00001390/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1391/// zeros. If so, the result pointer and the first operand have the same
1392/// value, just potentially different types.
1393bool GetElementPtrInst::hasAllZeroIndices() const {
1394 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1395 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1396 if (!CI->isZero()) return false;
1397 } else {
1398 return false;
1399 }
1400 }
1401 return true;
1402}
1403
Chris Lattner27058292007-04-27 20:35:56 +00001404/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1405/// constant integers. If so, the result pointer and the first operand have
1406/// a constant offset between them.
1407bool GetElementPtrInst::hasAllConstantIndices() const {
1408 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1409 if (!isa<ConstantInt>(getOperand(i)))
1410 return false;
1411 }
1412 return true;
1413}
1414
Dan Gohman1b849082009-09-07 23:54:19 +00001415void GetElementPtrInst::setIsInBounds(bool B) {
1416 cast<GEPOperator>(this)->setIsInBounds(B);
1417}
Chris Lattner45f15572007-04-14 00:12:57 +00001418
Nick Lewycky28a5f252009-09-27 21:33:04 +00001419bool GetElementPtrInst::isInBounds() const {
1420 return cast<GEPOperator>(this)->isInBounds();
1421}
1422
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001423//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001424// ExtractElementInst Implementation
1425//===----------------------------------------------------------------------===//
1426
1427ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001428 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001429 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001430 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001431 ExtractElement,
1432 OperandTraits<ExtractElementInst>::op_begin(this),
1433 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001434 assert(isValidOperands(Val, Index) &&
1435 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001436 Op<0>() = Val;
1437 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001438 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001439}
1440
1441ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001442 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001443 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001444 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001445 ExtractElement,
1446 OperandTraits<ExtractElementInst>::op_begin(this),
1447 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001448 assert(isValidOperands(Val, Index) &&
1449 "Invalid extractelement instruction operands!");
1450
Gabor Greif2d3024d2008-05-26 21:33:52 +00001451 Op<0>() = Val;
1452 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001453 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001454}
1455
Chris Lattner65511ff2006-10-05 06:24:58 +00001456
Chris Lattner54865b32006-04-08 04:05:48 +00001457bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001458 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001459 return false;
1460 return true;
1461}
1462
1463
Robert Bocchino23004482006-01-10 19:05:34 +00001464//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001465// InsertElementInst Implementation
1466//===----------------------------------------------------------------------===//
1467
Chris Lattner54865b32006-04-08 04:05:48 +00001468InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001469 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001470 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001471 : Instruction(Vec->getType(), InsertElement,
1472 OperandTraits<InsertElementInst>::op_begin(this),
1473 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001474 assert(isValidOperands(Vec, Elt, Index) &&
1475 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001476 Op<0>() = Vec;
1477 Op<1>() = Elt;
1478 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001479 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001480}
1481
Chris Lattner54865b32006-04-08 04:05:48 +00001482InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001483 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001484 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001485 : Instruction(Vec->getType(), InsertElement,
1486 OperandTraits<InsertElementInst>::op_begin(this),
1487 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001488 assert(isValidOperands(Vec, Elt, Index) &&
1489 "Invalid insertelement instruction operands!");
1490
Gabor Greif2d3024d2008-05-26 21:33:52 +00001491 Op<0>() = Vec;
1492 Op<1>() = Elt;
1493 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001494 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001495}
1496
Chris Lattner54865b32006-04-08 04:05:48 +00001497bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1498 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001499 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001500 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001501
Reid Spencerd84d35b2007-02-15 02:26:10 +00001502 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001503 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001504
Duncan Sands9dff9be2010-02-15 16:12:20 +00001505 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001506 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001507 return true;
1508}
1509
1510
Robert Bocchinoca27f032006-01-17 20:07:22 +00001511//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001512// ShuffleVectorInst Implementation
1513//===----------------------------------------------------------------------===//
1514
1515ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001516 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001517 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001518: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001519 cast<VectorType>(Mask->getType())->getNumElements()),
1520 ShuffleVector,
1521 OperandTraits<ShuffleVectorInst>::op_begin(this),
1522 OperandTraits<ShuffleVectorInst>::operands(this),
1523 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001524 assert(isValidOperands(V1, V2, Mask) &&
1525 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001526 Op<0>() = V1;
1527 Op<1>() = V2;
1528 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001529 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001530}
1531
1532ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001533 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001534 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001535: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1536 cast<VectorType>(Mask->getType())->getNumElements()),
1537 ShuffleVector,
1538 OperandTraits<ShuffleVectorInst>::op_begin(this),
1539 OperandTraits<ShuffleVectorInst>::operands(this),
1540 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001541 assert(isValidOperands(V1, V2, Mask) &&
1542 "Invalid shuffle vector instruction operands!");
1543
Gabor Greif2d3024d2008-05-26 21:33:52 +00001544 Op<0>() = V1;
1545 Op<1>() = V2;
1546 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001547 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001548}
1549
Mon P Wang25f01062008-11-10 04:46:22 +00001550bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001551 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001552 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001553 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001554 return false;
1555
Chris Lattner1dcb6542012-01-25 23:49:49 +00001556 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001557 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001558 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001559 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001560
1561 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001562 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1563 return true;
1564
Nate Begeman60a31c32010-08-13 00:16:46 +00001565 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001566 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001567 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001568 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1569 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001570 return false;
1571 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1572 return false;
1573 }
1574 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001575 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001576 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001577
1578 if (const ConstantDataSequential *CDS =
1579 dyn_cast<ConstantDataSequential>(Mask)) {
1580 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1581 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1582 if (CDS->getElementAsInteger(i) >= V1Size*2)
1583 return false;
1584 return true;
1585 }
1586
1587 // The bitcode reader can create a place holder for a forward reference
1588 // used as the shuffle mask. When this occurs, the shuffle mask will
1589 // fall into this case and fail. To avoid this error, do this bit of
1590 // ugliness to allow such a mask pass.
1591 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1592 if (CE->getOpcode() == Instruction::UserOp1)
1593 return true;
1594
1595 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001596}
1597
Chris Lattnerf724e342008-03-02 05:28:33 +00001598/// getMaskValue - Return the index from the shuffle mask for the specified
1599/// output result. This is either -1 if the element is undef or a number less
1600/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001601int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1602 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1603 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001604 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001605 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001606 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001607 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001608 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001609}
1610
Chris Lattner1dcb6542012-01-25 23:49:49 +00001611/// getShuffleMask - Return the full mask for this instruction, where each
1612/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001613void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1614 SmallVectorImpl<int> &Result) {
1615 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001616
Chris Lattnercf129702012-01-26 02:51:13 +00001617 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001618 for (unsigned i = 0; i != NumElts; ++i)
1619 Result.push_back(CDS->getElementAsInteger(i));
1620 return;
1621 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001622 for (unsigned i = 0; i != NumElts; ++i) {
1623 Constant *C = Mask->getAggregateElement(i);
1624 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001625 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001626 }
1627}
1628
1629
Dan Gohman12fce772008-05-15 19:50:34 +00001630//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001631// InsertValueInst Class
1632//===----------------------------------------------------------------------===//
1633
Jay Foad57aa6362011-07-13 10:26:04 +00001634void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1635 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001636 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001637
1638 // There's no fundamental reason why we require at least one index
1639 // (other than weirdness with &*IdxBegin being invalid; see
1640 // getelementptr's init routine for example). But there's no
1641 // present need to support it.
1642 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1643
1644 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001645 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001646 Op<0>() = Agg;
1647 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001648
Jay Foad57aa6362011-07-13 10:26:04 +00001649 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001650 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001651}
1652
1653InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001654 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001655 OperandTraits<InsertValueInst>::op_begin(this), 2),
1656 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001657 Op<0>() = IVI.getOperand(0);
1658 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001659 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001660}
1661
1662//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001663// ExtractValueInst Class
1664//===----------------------------------------------------------------------===//
1665
Jay Foad57aa6362011-07-13 10:26:04 +00001666void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001667 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001668
Jay Foad57aa6362011-07-13 10:26:04 +00001669 // There's no fundamental reason why we require at least one index.
1670 // But there's no present need to support it.
1671 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001672
Jay Foad57aa6362011-07-13 10:26:04 +00001673 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001674 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001675}
1676
1677ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001678 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001679 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001680 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001681}
1682
Dan Gohman12fce772008-05-15 19:50:34 +00001683// getIndexedType - Returns the type of the element that would be extracted
1684// with an extractvalue instruction with the specified parameters.
1685//
1686// A null type is returned if the indices are invalid for the specified
1687// pointer type.
1688//
Chris Lattner229907c2011-07-18 04:54:35 +00001689Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001690 ArrayRef<unsigned> Idxs) {
1691 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001692 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001693 // We can't use CompositeType::indexValid(Index) here.
1694 // indexValid() always returns true for arrays because getelementptr allows
1695 // out-of-bounds indices. Since we don't allow those for extractvalue and
1696 // insertvalue we need to check array indexing manually.
1697 // Since the only other types we can index into are struct types it's just
1698 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001699 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001700 if (Index >= AT->getNumElements())
1701 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001702 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001703 if (Index >= ST->getNumElements())
1704 return 0;
1705 } else {
1706 // Not a valid type to index into.
1707 return 0;
1708 }
1709
1710 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001711 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001712 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001713}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001714
1715//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001716// BinaryOperator Class
1717//===----------------------------------------------------------------------===//
1718
Chris Lattner2195fc42007-02-24 00:55:48 +00001719BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001720 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001721 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001722 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001723 OperandTraits<BinaryOperator>::op_begin(this),
1724 OperandTraits<BinaryOperator>::operands(this),
1725 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001726 Op<0>() = S1;
1727 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001728 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001729 setName(Name);
1730}
1731
1732BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001733 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001734 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001735 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001736 OperandTraits<BinaryOperator>::op_begin(this),
1737 OperandTraits<BinaryOperator>::operands(this),
1738 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001739 Op<0>() = S1;
1740 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001741 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001742 setName(Name);
1743}
1744
1745
1746void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001747 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001748 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001749 assert(LHS->getType() == RHS->getType() &&
1750 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001751#ifndef NDEBUG
1752 switch (iType) {
1753 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001754 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001755 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001756 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001757 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001758 "Tried to create an integer operation on a non-integer type!");
1759 break;
1760 case FAdd: case FSub:
1761 case FMul:
1762 assert(getType() == LHS->getType() &&
1763 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001764 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001765 "Tried to create a floating-point operation on a "
1766 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001767 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001768 case UDiv:
1769 case SDiv:
1770 assert(getType() == LHS->getType() &&
1771 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001772 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001773 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001774 "Incorrect operand type (not integer) for S/UDIV");
1775 break;
1776 case FDiv:
1777 assert(getType() == LHS->getType() &&
1778 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001779 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001780 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001781 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001782 case URem:
1783 case SRem:
1784 assert(getType() == LHS->getType() &&
1785 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001786 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001787 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001788 "Incorrect operand type (not integer) for S/UREM");
1789 break;
1790 case FRem:
1791 assert(getType() == LHS->getType() &&
1792 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001793 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001794 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001795 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001796 case Shl:
1797 case LShr:
1798 case AShr:
1799 assert(getType() == LHS->getType() &&
1800 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001801 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001802 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001803 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001804 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001805 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001806 case And: case Or:
1807 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001808 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001809 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001810 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001811 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001812 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001813 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001814 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001815 default:
1816 break;
1817 }
1818#endif
1819}
1820
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001821BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001822 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001823 Instruction *InsertBefore) {
1824 assert(S1->getType() == S2->getType() &&
1825 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001826 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001827}
1828
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001829BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001830 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001831 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001832 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001833 InsertAtEnd->getInstList().push_back(Res);
1834 return Res;
1835}
1836
Dan Gohman5476cfd2009-08-12 16:23:25 +00001837BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001838 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001839 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001840 return new BinaryOperator(Instruction::Sub,
1841 zero, Op,
1842 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001843}
1844
Dan Gohman5476cfd2009-08-12 16:23:25 +00001845BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001846 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001847 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001848 return new BinaryOperator(Instruction::Sub,
1849 zero, Op,
1850 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001851}
1852
Dan Gohman4ab44202009-12-18 02:58:50 +00001853BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1854 Instruction *InsertBefore) {
1855 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1856 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1857}
1858
1859BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1860 BasicBlock *InsertAtEnd) {
1861 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1862 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1863}
1864
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001865BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1866 Instruction *InsertBefore) {
1867 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1868 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1869}
1870
1871BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1872 BasicBlock *InsertAtEnd) {
1873 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1874 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1875}
1876
Dan Gohman5476cfd2009-08-12 16:23:25 +00001877BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001878 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001879 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001880 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001881 Op->getType(), Name, InsertBefore);
1882}
1883
Dan Gohman5476cfd2009-08-12 16:23:25 +00001884BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001885 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001886 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001887 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001888 Op->getType(), Name, InsertAtEnd);
1889}
1890
Dan Gohman5476cfd2009-08-12 16:23:25 +00001891BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001892 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001893 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001894 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001895 Op->getType(), Name, InsertBefore);
1896}
1897
Dan Gohman5476cfd2009-08-12 16:23:25 +00001898BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001899 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001900 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001901 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001902 Op->getType(), Name, InsertAtEnd);
1903}
1904
1905
1906// isConstantAllOnes - Helper function for several functions below
1907static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001908 if (const Constant *C = dyn_cast<Constant>(V))
1909 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001910 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001911}
1912
Owen Andersonbb2501b2009-07-13 22:18:28 +00001913bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001914 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1915 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001916 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1917 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001918 return false;
1919}
1920
Owen Andersonbb2501b2009-07-13 22:18:28 +00001921bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001922 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1923 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001924 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001925 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001926 return false;
1927}
1928
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001929bool BinaryOperator::isNot(const Value *V) {
1930 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1931 return (Bop->getOpcode() == Instruction::Xor &&
1932 (isConstantAllOnes(Bop->getOperand(1)) ||
1933 isConstantAllOnes(Bop->getOperand(0))));
1934 return false;
1935}
1936
Chris Lattner2c7d1772005-04-24 07:28:37 +00001937Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001938 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001939}
1940
Chris Lattner2c7d1772005-04-24 07:28:37 +00001941const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1942 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001943}
1944
Dan Gohmana5b96452009-06-04 22:49:04 +00001945Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001946 return cast<BinaryOperator>(BinOp)->getOperand(1);
1947}
1948
1949const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1950 return getFNegArgument(const_cast<Value*>(BinOp));
1951}
1952
Chris Lattner2c7d1772005-04-24 07:28:37 +00001953Value *BinaryOperator::getNotArgument(Value *BinOp) {
1954 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1955 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1956 Value *Op0 = BO->getOperand(0);
1957 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001958 if (isConstantAllOnes(Op0)) return Op1;
1959
1960 assert(isConstantAllOnes(Op1));
1961 return Op0;
1962}
1963
Chris Lattner2c7d1772005-04-24 07:28:37 +00001964const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1965 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001966}
1967
1968
1969// swapOperands - Exchange the two operands to this instruction. This
1970// instruction is safe to use on any binary instruction and does not
1971// modify the semantics of the instruction. If the instruction is
1972// order dependent (SetLT f.e.) the opcode is changed.
1973//
1974bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001975 if (!isCommutative())
1976 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001977 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001978 return false;
1979}
1980
Dan Gohman1b849082009-09-07 23:54:19 +00001981void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1982 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1983}
1984
1985void BinaryOperator::setHasNoSignedWrap(bool b) {
1986 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1987}
1988
1989void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001990 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001991}
1992
Nick Lewycky28a5f252009-09-27 21:33:04 +00001993bool BinaryOperator::hasNoUnsignedWrap() const {
1994 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1995}
1996
1997bool BinaryOperator::hasNoSignedWrap() const {
1998 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1999}
2000
2001bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002002 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002003}
2004
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002005//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002006// FPMathOperator Class
2007//===----------------------------------------------------------------------===//
2008
2009/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2010/// An accuracy of 0.0 means that the operation should be performed with the
2011/// default precision. A huge value is returned if the accuracy is 'fast'.
2012float FPMathOperator::getFPAccuracy() const {
2013 const MDNode *MD =
2014 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2015 if (!MD)
2016 return 0.0;
2017 Value *Op = MD->getOperand(0);
2018 if (const ConstantFP *Accuracy = dyn_cast<ConstantFP>(Op))
2019 return Accuracy->getValueAPF().convertToFloat();
2020 // If it's not a floating point number then it must be 'fast'.
2021 assert(isa<MDString>(Op) && cast<MDString>(Op)->getString() == "fast" &&
2022 "Expected the 'fast' keyword!");
2023 return HUGE_VALF;
2024}
2025
2026/// isFastFPAccuracy - Return true if the accuracy is 'fast'. This says that
2027/// speed is more important than accuracy.
2028bool FPMathOperator::isFastFPAccuracy() const {
2029 const MDNode *MD =
2030 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2031 if (!MD)
2032 return false;
2033 Value *Op = MD->getOperand(0);
2034 if (isa<ConstantFP>(Op))
2035 return false;
2036 // If it's not a floating point number then it must be 'fast'.
2037 assert(isa<MDString>(Op) && cast<MDString>(Op)->getString() == "fast" &&
2038 "Expected the 'fast' keyword!");
2039 return true;
2040}
2041
2042
2043//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002044// CastInst Class
2045//===----------------------------------------------------------------------===//
2046
David Blaikie3a15e142011-12-01 08:00:17 +00002047void CastInst::anchor() {}
2048
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002049// Just determine if this cast only deals with integral->integral conversion.
2050bool CastInst::isIntegerCast() const {
2051 switch (getOpcode()) {
2052 default: return false;
2053 case Instruction::ZExt:
2054 case Instruction::SExt:
2055 case Instruction::Trunc:
2056 return true;
2057 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002058 return getOperand(0)->getType()->isIntegerTy() &&
2059 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002061}
2062
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063bool CastInst::isLosslessCast() const {
2064 // Only BitCast can be lossless, exit fast if we're not BitCast
2065 if (getOpcode() != Instruction::BitCast)
2066 return false;
2067
2068 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002069 Type* SrcTy = getOperand(0)->getType();
2070 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002071 if (SrcTy == DstTy)
2072 return true;
2073
Reid Spencer8d9336d2006-12-31 05:26:44 +00002074 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002075 if (SrcTy->isPointerTy())
2076 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002077 return false; // Other types have no identity values
2078}
2079
2080/// This function determines if the CastInst does not require any bits to be
2081/// changed in order to effect the cast. Essentially, it identifies cases where
2082/// no code gen is necessary for the cast, hence the name no-op cast. For
2083/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002084/// # bitcast i32* %x to i8*
2085/// # bitcast <2 x i32> %x to <4 x i16>
2086/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002087/// @brief Determine if the described cast is a no-op.
2088bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002089 Type *SrcTy,
2090 Type *DestTy,
2091 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002092 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002093 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 case Instruction::Trunc:
2095 case Instruction::ZExt:
2096 case Instruction::SExt:
2097 case Instruction::FPTrunc:
2098 case Instruction::FPExt:
2099 case Instruction::UIToFP:
2100 case Instruction::SIToFP:
2101 case Instruction::FPToUI:
2102 case Instruction::FPToSI:
2103 return false; // These always modify bits
2104 case Instruction::BitCast:
2105 return true; // BitCast never modifies bits.
2106 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002107 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002108 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002109 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002110 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002111 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112 }
2113}
2114
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002115/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002116bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002117 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2118}
2119
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002120/// This function determines if a pair of casts can be eliminated and what
2121/// opcode should be used in the elimination. This assumes that there are two
2122/// instructions like this:
2123/// * %F = firstOpcode SrcTy %x to MidTy
2124/// * %S = secondOpcode MidTy %F to DstTy
2125/// The function returns a resultOpcode so these two casts can be replaced with:
2126/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2127/// If no such cast is permited, the function returns 0.
2128unsigned CastInst::isEliminableCastPair(
2129 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002130 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002131 // Define the 144 possibilities for these two cast instructions. The values
2132 // in this matrix determine what to do in a given situation and select the
2133 // case in the switch below. The rows correspond to firstOp, the columns
2134 // correspond to secondOp. In looking at the table below, keep in mind
2135 // the following cast properties:
2136 //
2137 // Size Compare Source Destination
2138 // Operator Src ? Size Type Sign Type Sign
2139 // -------- ------------ ------------------- ---------------------
2140 // TRUNC > Integer Any Integral Any
2141 // ZEXT < Integral Unsigned Integer Any
2142 // SEXT < Integral Signed Integer Any
2143 // FPTOUI n/a FloatPt n/a Integral Unsigned
2144 // FPTOSI n/a FloatPt n/a Integral Signed
2145 // UITOFP n/a Integral Unsigned FloatPt n/a
2146 // SITOFP n/a Integral Signed FloatPt n/a
2147 // FPTRUNC > FloatPt n/a FloatPt n/a
2148 // FPEXT < FloatPt n/a FloatPt n/a
2149 // PTRTOINT n/a Pointer n/a Integral Unsigned
2150 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002151 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002152 //
2153 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002154 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2155 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002156 // of the produced value (we no longer know the top-part is all zeros).
2157 // Further this conversion is often much more expensive for typical hardware,
2158 // and causes issues when building libgcc. We disallow fptosi+sext for the
2159 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002160 const unsigned numCastOps =
2161 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2162 static const uint8_t CastResults[numCastOps][numCastOps] = {
2163 // T F F U S F F P I B -+
2164 // R Z S P P I I T P 2 N T |
2165 // U E E 2 2 2 2 R E I T C +- secondOp
2166 // N X X U S F F N X N 2 V |
2167 // C T T I I P P C T T P T -+
2168 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2169 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2170 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002171 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2172 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002173 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2174 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2175 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2176 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2177 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2178 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2179 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2180 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002181
2182 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002183 // merging. However, bitcast of A->B->A are allowed.
2184 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2185 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2186 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2187
2188 // Check if any of the bitcasts convert scalars<->vectors.
2189 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2190 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2191 // Unless we are bitcasing to the original type, disallow optimizations.
2192 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002193
2194 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2195 [secondOp-Instruction::CastOpsBegin];
2196 switch (ElimCase) {
2197 case 0:
2198 // categorically disallowed
2199 return 0;
2200 case 1:
2201 // allowed, use first cast's opcode
2202 return firstOp;
2203 case 2:
2204 // allowed, use second cast's opcode
2205 return secondOp;
2206 case 3:
2207 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002208 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002209 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002210 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211 return firstOp;
2212 return 0;
2213 case 4:
2214 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002215 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002216 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002217 return firstOp;
2218 return 0;
2219 case 5:
2220 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002221 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002222 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002223 return secondOp;
2224 return 0;
2225 case 6:
2226 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002227 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002228 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002229 return secondOp;
2230 return 0;
2231 case 7: {
2232 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002233 if (!IntPtrTy)
2234 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002235 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2236 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 if (MidSize >= PtrSize)
2238 return Instruction::BitCast;
2239 return 0;
2240 }
2241 case 8: {
2242 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2243 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2244 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002245 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2246 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002247 if (SrcSize == DstSize)
2248 return Instruction::BitCast;
2249 else if (SrcSize < DstSize)
2250 return firstOp;
2251 return secondOp;
2252 }
2253 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2254 return Instruction::ZExt;
2255 case 10:
2256 // fpext followed by ftrunc is allowed if the bit size returned to is
2257 // the same as the original, in which case its just a bitcast
2258 if (SrcTy == DstTy)
2259 return Instruction::BitCast;
2260 return 0; // If the types are not the same we can't eliminate it.
2261 case 11:
2262 // bitcast followed by ptrtoint is allowed as long as the bitcast
2263 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002264 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002265 return secondOp;
2266 return 0;
2267 case 12:
2268 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002269 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270 return firstOp;
2271 return 0;
2272 case 13: {
2273 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002274 if (!IntPtrTy)
2275 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002276 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2277 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2278 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279 if (SrcSize <= PtrSize && SrcSize == DstSize)
2280 return Instruction::BitCast;
2281 return 0;
2282 }
2283 case 99:
2284 // cast combination can't happen (error in input). This is for all cases
2285 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002286 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002287 default:
Craig Topperc514b542012-02-05 22:14:15 +00002288 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290}
2291
Chris Lattner229907c2011-07-18 04:54:35 +00002292CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002293 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002294 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295 // Construct and return the appropriate CastInst subclass
2296 switch (op) {
2297 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2298 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2299 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2300 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2301 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2302 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2303 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2304 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2305 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2306 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2307 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2308 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002309 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311}
2312
Chris Lattner229907c2011-07-18 04:54:35 +00002313CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002314 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002315 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316 // Construct and return the appropriate CastInst subclass
2317 switch (op) {
2318 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2319 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2320 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2321 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2322 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2323 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2324 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2325 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2326 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2327 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2328 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2329 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002330 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002331 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332}
2333
Chris Lattner229907c2011-07-18 04:54:35 +00002334CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002335 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002336 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002337 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002338 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2339 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002340}
2341
Chris Lattner229907c2011-07-18 04:54:35 +00002342CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002343 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002344 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002345 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002346 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2347 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002348}
2349
Chris Lattner229907c2011-07-18 04:54:35 +00002350CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002351 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002352 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002353 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002354 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2355 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002356}
2357
Chris Lattner229907c2011-07-18 04:54:35 +00002358CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002359 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002360 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002361 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002362 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2363 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002364}
2365
Chris Lattner229907c2011-07-18 04:54:35 +00002366CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002367 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002368 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002369 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002370 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2371 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002372}
2373
Chris Lattner229907c2011-07-18 04:54:35 +00002374CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002375 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002376 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002377 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002378 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2379 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002380}
2381
Chris Lattner229907c2011-07-18 04:54:35 +00002382CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002383 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002384 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002385 assert(S->getType()->isPointerTy() && "Invalid cast");
2386 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002387 "Invalid cast");
2388
Duncan Sands9dff9be2010-02-15 16:12:20 +00002389 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002390 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2391 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002392}
2393
2394/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002395CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002396 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002397 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002398 assert(S->getType()->isPointerTy() && "Invalid cast");
2399 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002400 "Invalid cast");
2401
Duncan Sands9dff9be2010-02-15 16:12:20 +00002402 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002403 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2404 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002405}
2406
Chris Lattner229907c2011-07-18 04:54:35 +00002407CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002408 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002409 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002410 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002411 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002412 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2413 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002414 Instruction::CastOps opcode =
2415 (SrcBits == DstBits ? Instruction::BitCast :
2416 (SrcBits > DstBits ? Instruction::Trunc :
2417 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002418 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002419}
2420
Chris Lattner229907c2011-07-18 04:54:35 +00002421CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002422 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002423 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002424 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002425 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002426 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2427 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002428 Instruction::CastOps opcode =
2429 (SrcBits == DstBits ? Instruction::BitCast :
2430 (SrcBits > DstBits ? Instruction::Trunc :
2431 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002432 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002433}
2434
Chris Lattner229907c2011-07-18 04:54:35 +00002435CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002436 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002437 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002438 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002439 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002440 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2441 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002442 Instruction::CastOps opcode =
2443 (SrcBits == DstBits ? Instruction::BitCast :
2444 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002445 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002446}
2447
Chris Lattner229907c2011-07-18 04:54:35 +00002448CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002449 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002450 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002451 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002452 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002453 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2454 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002455 Instruction::CastOps opcode =
2456 (SrcBits == DstBits ? Instruction::BitCast :
2457 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002458 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002459}
2460
Duncan Sands55e50902008-01-06 10:12:28 +00002461// Check whether it is valid to call getCastOpcode for these types.
2462// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002463bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002464 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2465 return false;
2466
2467 if (SrcTy == DestTy)
2468 return true;
2469
Chris Lattner229907c2011-07-18 04:54:35 +00002470 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2471 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002472 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2473 // An element by element cast. Valid if casting the elements is valid.
2474 SrcTy = SrcVecTy->getElementType();
2475 DestTy = DestVecTy->getElementType();
2476 }
2477
Duncan Sands55e50902008-01-06 10:12:28 +00002478 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002479 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2480 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002481
2482 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002483 if (DestTy->isIntegerTy()) { // Casting to integral
2484 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002485 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002486 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002487 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002488 } else if (SrcTy->isVectorTy()) { // Casting from vector
2489 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002490 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002491 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002492 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002493 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2494 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002495 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002496 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002497 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002498 } else if (SrcTy->isVectorTy()) { // Casting from vector
2499 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002500 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002501 return false;
2502 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002503 } else if (DestTy->isVectorTy()) { // Casting to vector
2504 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002505 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002506 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002507 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002508 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002509 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002510 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002511 return false;
2512 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002513 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002514 if (SrcTy->isVectorTy()) {
2515 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002516 } else {
2517 return false;
2518 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002519 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002520 return false;
2521 }
2522}
2523
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524// Provide a way to get a "cast" where the cast opcode is inferred from the
2525// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002526// logic in the castIsValid function below. This axiom should hold:
2527// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2528// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002530// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002532CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002533 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2534 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535
Duncan Sands55e50902008-01-06 10:12:28 +00002536 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2537 "Only first class types are castable!");
2538
Duncan Sandsa8514532011-05-18 07:13:41 +00002539 if (SrcTy == DestTy)
2540 return BitCast;
2541
Chris Lattner229907c2011-07-18 04:54:35 +00002542 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2543 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002544 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2545 // An element by element cast. Find the appropriate opcode based on the
2546 // element types.
2547 SrcTy = SrcVecTy->getElementType();
2548 DestTy = DestVecTy->getElementType();
2549 }
2550
2551 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002552 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2553 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002554
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002556 if (DestTy->isIntegerTy()) { // Casting to integral
2557 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558 if (DestBits < SrcBits)
2559 return Trunc; // int -> smaller int
2560 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002561 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562 return SExt; // signed -> SEXT
2563 else
2564 return ZExt; // unsigned -> ZEXT
2565 } else {
2566 return BitCast; // Same size, No-op cast
2567 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002568 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002569 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570 return FPToSI; // FP -> sint
2571 else
2572 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002573 } else if (SrcTy->isVectorTy()) {
2574 assert(DestBits == SrcBits &&
2575 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576 return BitCast; // Same size, no-op cast
2577 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002578 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579 "Casting from a value that is not first-class type");
2580 return PtrToInt; // ptr -> int
2581 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002582 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2583 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002584 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585 return SIToFP; // sint -> FP
2586 else
2587 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002588 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589 if (DestBits < SrcBits) {
2590 return FPTrunc; // FP -> smaller FP
2591 } else if (DestBits > SrcBits) {
2592 return FPExt; // FP -> larger FP
2593 } else {
2594 return BitCast; // same size, no-op cast
2595 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002596 } else if (SrcTy->isVectorTy()) {
2597 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002598 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002599 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002601 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002602 } else if (DestTy->isVectorTy()) {
2603 assert(DestBits == SrcBits &&
2604 "Illegal cast to vector (wrong type or size)");
2605 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002606 } else if (DestTy->isPointerTy()) {
2607 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002609 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002612 llvm_unreachable("Casting pointer to other than pointer or int");
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
Dale Johannesendd224d22010-09-30 23:57:10 +00002617 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002618 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002620 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621}
2622
2623//===----------------------------------------------------------------------===//
2624// CastInst SubClass Constructors
2625//===----------------------------------------------------------------------===//
2626
2627/// Check that the construction parameters for a CastInst are correct. This
2628/// could be broken out into the separate constructors but it is useful to have
2629/// it in one place and to eliminate the redundant code for getting the sizes
2630/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002631bool
Chris Lattner229907c2011-07-18 04:54:35 +00002632CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633
2634 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002635 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002636 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2637 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002638 return false;
2639
2640 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002641 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2642 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643
Duncan Sands7f646562011-05-18 09:21:57 +00002644 // If these are vector types, get the lengths of the vectors (using zero for
2645 // scalar types means that checking that vector lengths match also checks that
2646 // scalars are not being converted to vectors or vectors to scalars).
2647 unsigned SrcLength = SrcTy->isVectorTy() ?
2648 cast<VectorType>(SrcTy)->getNumElements() : 0;
2649 unsigned DstLength = DstTy->isVectorTy() ?
2650 cast<VectorType>(DstTy)->getNumElements() : 0;
2651
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652 // Switch on the opcode provided
2653 switch (op) {
2654 default: return false; // This is an input error
2655 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002656 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2657 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002658 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002659 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2660 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002662 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2663 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002664 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002665 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2666 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002667 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002668 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2669 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002670 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002672 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2673 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002675 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002676 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2677 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002678 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002679 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002680 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002681 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2682 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2683 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002684 return SrcTy->getScalarType()->isPointerTy() &&
2685 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002686 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002687 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002688 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002689 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2690 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2691 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002692 return SrcTy->getScalarType()->isIntegerTy() &&
2693 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002694 case Instruction::BitCast:
2695 // BitCast implies a no-op cast of type only. No bits change.
2696 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002697 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002698 return false;
2699
Duncan Sands55e50902008-01-06 10:12:28 +00002700 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002701 // these cases, the cast is okay if the source and destination bit widths
2702 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002703 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002704 }
2705}
2706
2707TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002708 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002709) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002710 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002711}
2712
2713TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002714 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002715) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002716 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002717}
2718
2719ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002720 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002721) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002722 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002723}
2724
2725ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002726 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002727) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002728 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002729}
2730SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002731 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002732) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002733 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002734}
2735
Jeff Cohencc08c832006-12-02 02:22:01 +00002736SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002737 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002738) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002739 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002740}
2741
2742FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002743 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002744) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002745 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002746}
2747
2748FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002749 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002750) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002751 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002752}
2753
2754FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002755 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002756) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002757 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002758}
2759
2760FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002761 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002762) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002763 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002764}
2765
2766UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002767 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002768) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002769 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002770}
2771
2772UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002773 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002774) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002775 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002776}
2777
2778SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002779 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002780) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002781 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002782}
2783
2784SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002785 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002786) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002787 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002788}
2789
2790FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002791 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002792) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002793 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002794}
2795
2796FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002797 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002798) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002799 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002800}
2801
2802FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002803 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002804) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002805 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002806}
2807
2808FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002809 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002810) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002811 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002812}
2813
2814PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002815 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002816) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002817 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002818}
2819
2820PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002821 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002822) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002823 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002824}
2825
2826IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002827 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002828) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002829 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002830}
2831
2832IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002833 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002834) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002835 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002836}
2837
2838BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002839 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002840) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002841 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002842}
2843
2844BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002845 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002846) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002847 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002848}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002849
2850//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002851// CmpInst Classes
2852//===----------------------------------------------------------------------===//
2853
Chris Lattneraec33da2010-01-22 06:25:37 +00002854void CmpInst::Anchor() const {}
2855
Chris Lattner229907c2011-07-18 04:54:35 +00002856CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002857 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002858 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002859 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002860 OperandTraits<CmpInst>::op_begin(this),
2861 OperandTraits<CmpInst>::operands(this),
2862 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002863 Op<0>() = LHS;
2864 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002865 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002866 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002867}
Gabor Greiff6caff662008-05-10 08:32:32 +00002868
Chris Lattner229907c2011-07-18 04:54:35 +00002869CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002870 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002871 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002872 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002873 OperandTraits<CmpInst>::op_begin(this),
2874 OperandTraits<CmpInst>::operands(this),
2875 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002876 Op<0>() = LHS;
2877 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002878 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002879 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002880}
2881
2882CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002883CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002884 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002885 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002886 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002887 if (InsertBefore)
2888 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2889 S1, S2, Name);
2890 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002891 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002892 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002893 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002894
2895 if (InsertBefore)
2896 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2897 S1, S2, Name);
2898 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002899 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002900 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002901}
2902
2903CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002904CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002905 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002906 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002907 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2908 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002909 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002910 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2911 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002912}
2913
2914void CmpInst::swapOperands() {
2915 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2916 IC->swapOperands();
2917 else
2918 cast<FCmpInst>(this)->swapOperands();
2919}
2920
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002921bool CmpInst::isCommutative() const {
2922 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002923 return IC->isCommutative();
2924 return cast<FCmpInst>(this)->isCommutative();
2925}
2926
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002927bool CmpInst::isEquality() const {
2928 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002929 return IC->isEquality();
2930 return cast<FCmpInst>(this)->isEquality();
2931}
2932
2933
Dan Gohman4e724382008-05-31 02:47:54 +00002934CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002935 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002936 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002937 case ICMP_EQ: return ICMP_NE;
2938 case ICMP_NE: return ICMP_EQ;
2939 case ICMP_UGT: return ICMP_ULE;
2940 case ICMP_ULT: return ICMP_UGE;
2941 case ICMP_UGE: return ICMP_ULT;
2942 case ICMP_ULE: return ICMP_UGT;
2943 case ICMP_SGT: return ICMP_SLE;
2944 case ICMP_SLT: return ICMP_SGE;
2945 case ICMP_SGE: return ICMP_SLT;
2946 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002947
Dan Gohman4e724382008-05-31 02:47:54 +00002948 case FCMP_OEQ: return FCMP_UNE;
2949 case FCMP_ONE: return FCMP_UEQ;
2950 case FCMP_OGT: return FCMP_ULE;
2951 case FCMP_OLT: return FCMP_UGE;
2952 case FCMP_OGE: return FCMP_ULT;
2953 case FCMP_OLE: return FCMP_UGT;
2954 case FCMP_UEQ: return FCMP_ONE;
2955 case FCMP_UNE: return FCMP_OEQ;
2956 case FCMP_UGT: return FCMP_OLE;
2957 case FCMP_ULT: return FCMP_OGE;
2958 case FCMP_UGE: return FCMP_OLT;
2959 case FCMP_ULE: return FCMP_OGT;
2960 case FCMP_ORD: return FCMP_UNO;
2961 case FCMP_UNO: return FCMP_ORD;
2962 case FCMP_TRUE: return FCMP_FALSE;
2963 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002964 }
2965}
2966
Reid Spencer266e42b2006-12-23 06:05:41 +00002967ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2968 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002969 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002970 case ICMP_EQ: case ICMP_NE:
2971 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2972 return pred;
2973 case ICMP_UGT: return ICMP_SGT;
2974 case ICMP_ULT: return ICMP_SLT;
2975 case ICMP_UGE: return ICMP_SGE;
2976 case ICMP_ULE: return ICMP_SLE;
2977 }
2978}
2979
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002980ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2981 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002982 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002983 case ICMP_EQ: case ICMP_NE:
2984 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2985 return pred;
2986 case ICMP_SGT: return ICMP_UGT;
2987 case ICMP_SLT: return ICMP_ULT;
2988 case ICMP_SGE: return ICMP_UGE;
2989 case ICMP_SLE: return ICMP_ULE;
2990 }
2991}
2992
Reid Spencer0286bc12007-02-28 22:00:54 +00002993/// Initialize a set of values that all satisfy the condition with C.
2994///
2995ConstantRange
2996ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2997 APInt Lower(C);
2998 APInt Upper(C);
2999 uint32_t BitWidth = C.getBitWidth();
3000 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003001 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00003002 case ICmpInst::ICMP_EQ: Upper++; break;
3003 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003004 case ICmpInst::ICMP_ULT:
3005 Lower = APInt::getMinValue(BitWidth);
3006 // Check for an empty-set condition.
3007 if (Lower == Upper)
3008 return ConstantRange(BitWidth, /*isFullSet=*/false);
3009 break;
3010 case ICmpInst::ICMP_SLT:
3011 Lower = APInt::getSignedMinValue(BitWidth);
3012 // Check for an empty-set condition.
3013 if (Lower == Upper)
3014 return ConstantRange(BitWidth, /*isFullSet=*/false);
3015 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003016 case ICmpInst::ICMP_UGT:
3017 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003018 // Check for an empty-set condition.
3019 if (Lower == Upper)
3020 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003021 break;
3022 case ICmpInst::ICMP_SGT:
3023 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003024 // Check for an empty-set condition.
3025 if (Lower == Upper)
3026 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003027 break;
3028 case ICmpInst::ICMP_ULE:
3029 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003030 // Check for a full-set condition.
3031 if (Lower == Upper)
3032 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003033 break;
3034 case ICmpInst::ICMP_SLE:
3035 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003036 // Check for a full-set condition.
3037 if (Lower == Upper)
3038 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003039 break;
3040 case ICmpInst::ICMP_UGE:
3041 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003042 // Check for a full-set condition.
3043 if (Lower == Upper)
3044 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003045 break;
3046 case ICmpInst::ICMP_SGE:
3047 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003048 // Check for a full-set condition.
3049 if (Lower == Upper)
3050 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003051 break;
3052 }
3053 return ConstantRange(Lower, Upper);
3054}
3055
Dan Gohman4e724382008-05-31 02:47:54 +00003056CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003057 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003058 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003059 case ICMP_EQ: case ICMP_NE:
3060 return pred;
3061 case ICMP_SGT: return ICMP_SLT;
3062 case ICMP_SLT: return ICMP_SGT;
3063 case ICMP_SGE: return ICMP_SLE;
3064 case ICMP_SLE: return ICMP_SGE;
3065 case ICMP_UGT: return ICMP_ULT;
3066 case ICMP_ULT: return ICMP_UGT;
3067 case ICMP_UGE: return ICMP_ULE;
3068 case ICMP_ULE: return ICMP_UGE;
3069
Reid Spencerd9436b62006-11-20 01:22:35 +00003070 case FCMP_FALSE: case FCMP_TRUE:
3071 case FCMP_OEQ: case FCMP_ONE:
3072 case FCMP_UEQ: case FCMP_UNE:
3073 case FCMP_ORD: case FCMP_UNO:
3074 return pred;
3075 case FCMP_OGT: return FCMP_OLT;
3076 case FCMP_OLT: return FCMP_OGT;
3077 case FCMP_OGE: return FCMP_OLE;
3078 case FCMP_OLE: return FCMP_OGE;
3079 case FCMP_UGT: return FCMP_ULT;
3080 case FCMP_ULT: return FCMP_UGT;
3081 case FCMP_UGE: return FCMP_ULE;
3082 case FCMP_ULE: return FCMP_UGE;
3083 }
3084}
3085
Reid Spencer266e42b2006-12-23 06:05:41 +00003086bool CmpInst::isUnsigned(unsigned short predicate) {
3087 switch (predicate) {
3088 default: return false;
3089 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3090 case ICmpInst::ICMP_UGE: return true;
3091 }
3092}
3093
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003094bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003095 switch (predicate) {
3096 default: return false;
3097 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3098 case ICmpInst::ICMP_SGE: return true;
3099 }
3100}
3101
3102bool CmpInst::isOrdered(unsigned short predicate) {
3103 switch (predicate) {
3104 default: return false;
3105 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3106 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3107 case FCmpInst::FCMP_ORD: return true;
3108 }
3109}
3110
3111bool CmpInst::isUnordered(unsigned short predicate) {
3112 switch (predicate) {
3113 default: return false;
3114 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3115 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3116 case FCmpInst::FCMP_UNO: return true;
3117 }
3118}
3119
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003120bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3121 switch(predicate) {
3122 default: return false;
3123 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3124 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3125 }
3126}
3127
3128bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3129 switch(predicate) {
3130 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3131 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3132 default: return false;
3133 }
3134}
3135
3136
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003137//===----------------------------------------------------------------------===//
3138// SwitchInst Implementation
3139//===----------------------------------------------------------------------===//
3140
Chris Lattnerbaf00152010-11-17 05:41:46 +00003141void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3142 assert(Value && Default && NumReserved);
3143 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003144 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003145 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003146
Gabor Greif2d3024d2008-05-26 21:33:52 +00003147 OperandList[0] = Value;
3148 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003149}
3150
Chris Lattner2195fc42007-02-24 00:55:48 +00003151/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3152/// switch on and a default destination. The number of additional cases can
3153/// be specified here to make memory allocation more efficient. This
3154/// constructor can also autoinsert before another instruction.
3155SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3156 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003157 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3158 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003159 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003160}
3161
3162/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3163/// switch on and a default destination. The number of additional cases can
3164/// be specified here to make memory allocation more efficient. This
3165/// constructor also autoinserts at the end of the specified BasicBlock.
3166SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3167 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003168 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3169 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003170 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003171}
3172
Misha Brukmanb1c93172005-04-21 23:48:37 +00003173SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003174 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3175 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3176 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003177 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003178 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003179 OL[i] = InOL[i];
3180 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003181 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003182 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003183}
3184
Gordon Henriksen14a55692007-12-10 02:14:30 +00003185SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003186 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003187}
3188
3189
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003190/// addCase - Add an entry to the switch instruction...
3191///
Chris Lattner47ac1872005-02-24 05:32:09 +00003192void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003193 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003194 unsigned OpNo = NumOperands;
3195 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003196 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003197 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003198 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003199 NumOperands = OpNo+2;
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003200 CaseIt Case(this, NewCaseIdx);
3201 Case.setValue(OnVal);
3202 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003203}
3204
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003205/// removeCase - This method removes the specified case and its successor
3206/// from the switch instruction.
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003207void SwitchInst::removeCase(CaseIt i) {
3208 unsigned idx = i.getCaseIndex();
3209
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003210 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003211
3212 unsigned NumOps = getNumOperands();
3213 Use *OL = OperandList;
3214
Jay Foad14277722011-02-01 09:22:34 +00003215 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003216 if (2 + (idx + 1) * 2 != NumOps) {
3217 OL[2 + idx * 2] = OL[NumOps - 2];
3218 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003219 }
3220
3221 // Nuke the last value.
3222 OL[NumOps-2].set(0);
3223 OL[NumOps-2+1].set(0);
3224 NumOperands = NumOps-2;
3225}
3226
Jay Foade98f29d2011-04-01 08:00:58 +00003227/// growOperands - grow operands - This grows the operand list in response
3228/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003229///
Jay Foade98f29d2011-04-01 08:00:58 +00003230void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003231 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003232 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003233
3234 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003235 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003236 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003237 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003238 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003239 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003240 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003241 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003242}
3243
3244
3245BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3246 return getSuccessor(idx);
3247}
3248unsigned SwitchInst::getNumSuccessorsV() const {
3249 return getNumSuccessors();
3250}
3251void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3252 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003253}
Chris Lattnerf22be932004-10-15 23:52:53 +00003254
Chris Lattner3ed871f2009-10-27 19:13:16 +00003255//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003256// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003257//===----------------------------------------------------------------------===//
3258
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003259void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003260 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003261 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003262 ReservedSpace = 1+NumDests;
3263 NumOperands = 1;
3264 OperandList = allocHungoffUses(ReservedSpace);
3265
3266 OperandList[0] = Address;
3267}
3268
3269
Jay Foade98f29d2011-04-01 08:00:58 +00003270/// growOperands - grow operands - This grows the operand list in response
3271/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003272///
Jay Foade98f29d2011-04-01 08:00:58 +00003273void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003274 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003275 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003276
3277 ReservedSpace = NumOps;
3278 Use *NewOps = allocHungoffUses(NumOps);
3279 Use *OldOps = OperandList;
3280 for (unsigned i = 0; i != e; ++i)
3281 NewOps[i] = OldOps[i];
3282 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003283 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003284}
3285
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003286IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3287 Instruction *InsertBefore)
3288: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003289 0, 0, InsertBefore) {
3290 init(Address, NumCases);
3291}
3292
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003293IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3294 BasicBlock *InsertAtEnd)
3295: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003296 0, 0, InsertAtEnd) {
3297 init(Address, NumCases);
3298}
3299
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003300IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3301 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003302 allocHungoffUses(IBI.getNumOperands()),
3303 IBI.getNumOperands()) {
3304 Use *OL = OperandList, *InOL = IBI.OperandList;
3305 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3306 OL[i] = InOL[i];
3307 SubclassOptionalData = IBI.SubclassOptionalData;
3308}
3309
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003310IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003311 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003312}
3313
3314/// addDestination - Add a destination.
3315///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003316void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003317 unsigned OpNo = NumOperands;
3318 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003319 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003320 // Initialize some new operands.
3321 assert(OpNo < ReservedSpace && "Growing didn't work!");
3322 NumOperands = OpNo+1;
3323 OperandList[OpNo] = DestBB;
3324}
3325
3326/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003327/// indirectbr instruction.
3328void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003329 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3330
3331 unsigned NumOps = getNumOperands();
3332 Use *OL = OperandList;
3333
3334 // Replace this value with the last one.
3335 OL[idx+1] = OL[NumOps-1];
3336
3337 // Nuke the last value.
3338 OL[NumOps-1].set(0);
3339 NumOperands = NumOps-1;
3340}
3341
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003342BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003343 return getSuccessor(idx);
3344}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003345unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003346 return getNumSuccessors();
3347}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003348void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003349 setSuccessor(idx, B);
3350}
3351
3352//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003353// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003354//===----------------------------------------------------------------------===//
3355
Chris Lattnerf22be932004-10-15 23:52:53 +00003356// Define these methods here so vtables don't get emitted into every translation
3357// unit that uses these classes.
3358
Devang Patel11cf3f42009-10-27 22:16:29 +00003359GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3360 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003361}
3362
Devang Patel11cf3f42009-10-27 22:16:29 +00003363BinaryOperator *BinaryOperator::clone_impl() const {
3364 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003365}
3366
Devang Patel11cf3f42009-10-27 22:16:29 +00003367FCmpInst* FCmpInst::clone_impl() const {
3368 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003369}
3370
Devang Patel11cf3f42009-10-27 22:16:29 +00003371ICmpInst* ICmpInst::clone_impl() const {
3372 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003373}
3374
Devang Patel11cf3f42009-10-27 22:16:29 +00003375ExtractValueInst *ExtractValueInst::clone_impl() const {
3376 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003377}
3378
Devang Patel11cf3f42009-10-27 22:16:29 +00003379InsertValueInst *InsertValueInst::clone_impl() const {
3380 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003381}
3382
Devang Patel11cf3f42009-10-27 22:16:29 +00003383AllocaInst *AllocaInst::clone_impl() const {
3384 return new AllocaInst(getAllocatedType(),
3385 (Value*)getOperand(0),
3386 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003387}
3388
Devang Patel11cf3f42009-10-27 22:16:29 +00003389LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003390 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3391 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003392}
3393
Devang Patel11cf3f42009-10-27 22:16:29 +00003394StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003395 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003396 getAlignment(), getOrdering(), getSynchScope());
3397
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003398}
3399
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003400AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3401 AtomicCmpXchgInst *Result =
3402 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3403 getOrdering(), getSynchScope());
3404 Result->setVolatile(isVolatile());
3405 return Result;
3406}
3407
3408AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3409 AtomicRMWInst *Result =
3410 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3411 getOrdering(), getSynchScope());
3412 Result->setVolatile(isVolatile());
3413 return Result;
3414}
3415
Eli Friedmanfee02c62011-07-25 23:16:38 +00003416FenceInst *FenceInst::clone_impl() const {
3417 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3418}
3419
Devang Patel11cf3f42009-10-27 22:16:29 +00003420TruncInst *TruncInst::clone_impl() const {
3421 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003422}
3423
Devang Patel11cf3f42009-10-27 22:16:29 +00003424ZExtInst *ZExtInst::clone_impl() const {
3425 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003426}
3427
Devang Patel11cf3f42009-10-27 22:16:29 +00003428SExtInst *SExtInst::clone_impl() const {
3429 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003430}
3431
Devang Patel11cf3f42009-10-27 22:16:29 +00003432FPTruncInst *FPTruncInst::clone_impl() const {
3433 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003434}
3435
Devang Patel11cf3f42009-10-27 22:16:29 +00003436FPExtInst *FPExtInst::clone_impl() const {
3437 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003438}
3439
Devang Patel11cf3f42009-10-27 22:16:29 +00003440UIToFPInst *UIToFPInst::clone_impl() const {
3441 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003442}
3443
Devang Patel11cf3f42009-10-27 22:16:29 +00003444SIToFPInst *SIToFPInst::clone_impl() const {
3445 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003446}
3447
Devang Patel11cf3f42009-10-27 22:16:29 +00003448FPToUIInst *FPToUIInst::clone_impl() const {
3449 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003450}
3451
Devang Patel11cf3f42009-10-27 22:16:29 +00003452FPToSIInst *FPToSIInst::clone_impl() const {
3453 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003454}
3455
Devang Patel11cf3f42009-10-27 22:16:29 +00003456PtrToIntInst *PtrToIntInst::clone_impl() const {
3457 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003458}
3459
Devang Patel11cf3f42009-10-27 22:16:29 +00003460IntToPtrInst *IntToPtrInst::clone_impl() const {
3461 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003462}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003463
Devang Patel11cf3f42009-10-27 22:16:29 +00003464BitCastInst *BitCastInst::clone_impl() const {
3465 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003466}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003467
Devang Patel11cf3f42009-10-27 22:16:29 +00003468CallInst *CallInst::clone_impl() const {
3469 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003470}
3471
Devang Patel11cf3f42009-10-27 22:16:29 +00003472SelectInst *SelectInst::clone_impl() const {
3473 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003474}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003475
Devang Patel11cf3f42009-10-27 22:16:29 +00003476VAArgInst *VAArgInst::clone_impl() const {
3477 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003478}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003479
Devang Patel11cf3f42009-10-27 22:16:29 +00003480ExtractElementInst *ExtractElementInst::clone_impl() const {
3481 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003482}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003483
Devang Patel11cf3f42009-10-27 22:16:29 +00003484InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003485 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003486}
3487
Devang Patel11cf3f42009-10-27 22:16:29 +00003488ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003489 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003490}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003491
Devang Patel11cf3f42009-10-27 22:16:29 +00003492PHINode *PHINode::clone_impl() const {
3493 return new PHINode(*this);
3494}
3495
Bill Wendlingfae14752011-08-12 20:24:12 +00003496LandingPadInst *LandingPadInst::clone_impl() const {
3497 return new LandingPadInst(*this);
3498}
3499
Devang Patel11cf3f42009-10-27 22:16:29 +00003500ReturnInst *ReturnInst::clone_impl() const {
3501 return new(getNumOperands()) ReturnInst(*this);
3502}
3503
3504BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003505 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003506}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003507
Devang Patel11cf3f42009-10-27 22:16:29 +00003508SwitchInst *SwitchInst::clone_impl() const {
3509 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003510}
3511
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003512IndirectBrInst *IndirectBrInst::clone_impl() const {
3513 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003514}
3515
3516
Devang Patel11cf3f42009-10-27 22:16:29 +00003517InvokeInst *InvokeInst::clone_impl() const {
3518 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003519}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003520
Bill Wendlingf891bf82011-07-31 06:30:59 +00003521ResumeInst *ResumeInst::clone_impl() const {
3522 return new(1) ResumeInst(*this);
3523}
3524
Devang Patel11cf3f42009-10-27 22:16:29 +00003525UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003526 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003527 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003528}