blob: 17ab34f4f8fbe38b6d608c3c145ac69a72bf73c4 [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000017#include "llvm/Constants.h"
Chandler Carruth1e140532012-12-11 10:29:10 +000018#include "llvm/DataLayout.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000019#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000021#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000022#include "llvm/Operator.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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
65
Chris Lattner229907c2011-07-18 04:54:35 +000066 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000067 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000068 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000069 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000070 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Chris Lattner88107952008-12-29 00:12:50 +000071 if (ET == 0)
72 return "selected values for vector select must be vectors";
73 if (ET->getNumElements() != VT->getNumElements())
74 return "vector select requires selected vectors to have "
75 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000076 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000077 return "select condition must be i1 or <n x i1>";
78 }
79 return 0;
80}
81
82
83//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000084// PHINode Class
85//===----------------------------------------------------------------------===//
86
87PHINode::PHINode(const PHINode &PN)
88 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000089 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000090 ReservedSpace(PN.getNumOperands()) {
Jay Foad61ea0e42011-06-23 09:09:15 +000091 std::copy(PN.op_begin(), PN.op_end(), op_begin());
92 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000093 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000094}
95
Gordon Henriksen14a55692007-12-10 02:14:30 +000096PHINode::~PHINode() {
Jay Foadbbb91f22011-01-16 15:30:52 +000097 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +000098}
99
Jay Foad61ea0e42011-06-23 09:09:15 +0000100Use *PHINode::allocHungoffUses(unsigned N) const {
101 // Allocate the array of Uses of the incoming values, followed by a pointer
102 // (with bottom bit set) to the User, followed by the array of pointers to
103 // the incoming basic blocks.
104 size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
105 + N * sizeof(BasicBlock*);
106 Use *Begin = static_cast<Use*>(::operator new(size));
107 Use *End = Begin + N;
108 (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1);
109 return Use::initTags(Begin, End);
110}
111
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000112// removeIncomingValue - Remove an incoming value. This is useful if a
113// predecessor basic block is deleted.
114Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000115 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000116
117 // Move everything after this operand down.
118 //
119 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000120 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000121 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000122 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
123 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000124
125 // Nuke the last value.
Jay Foad61ea0e42011-06-23 09:09:15 +0000126 Op<-1>().set(0);
127 --NumOperands;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128
129 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000130 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000132 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000133 eraseFromParent();
134 }
135 return Removed;
136}
137
Jay Foade98f29d2011-04-01 08:00:58 +0000138/// growOperands - grow operands - This grows the operand list in response
139/// to a push_back style of operation. This grows the number of ops by 1.5
140/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000141///
Jay Foade98f29d2011-04-01 08:00:58 +0000142void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000143 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000144 unsigned NumOps = e + e / 2;
145 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
146
147 Use *OldOps = op_begin();
148 BasicBlock **OldBlocks = block_begin();
Jay Foade03c05c2011-06-20 14:38:01 +0000149
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000150 ReservedSpace = NumOps;
Jay Foad61ea0e42011-06-23 09:09:15 +0000151 OperandList = allocHungoffUses(ReservedSpace);
152
153 std::copy(OldOps, OldOps + e, op_begin());
154 std::copy(OldBlocks, OldBlocks + e, block_begin());
155
Jay Foadbbb91f22011-01-16 15:30:52 +0000156 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157}
158
Nate Begemanb3923212005-08-04 23:24:19 +0000159/// hasConstantValue - If the specified PHI node always merges together the same
160/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000161Value *PHINode::hasConstantValue() const {
162 // Exploit the fact that phi nodes always have at least one entry.
163 Value *ConstantValue = getIncomingValue(0);
164 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000165 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
166 if (ConstantValue != this)
167 return 0; // Incoming values not all the same.
168 // The case where the first value is this PHI.
169 ConstantValue = getIncomingValue(i);
170 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000171 if (ConstantValue == this)
172 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000173 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000174}
175
Bill Wendlingfae14752011-08-12 20:24:12 +0000176//===----------------------------------------------------------------------===//
177// LandingPadInst Implementation
178//===----------------------------------------------------------------------===//
179
180LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
181 unsigned NumReservedValues, const Twine &NameStr,
182 Instruction *InsertBefore)
183 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) {
184 init(PersonalityFn, 1 + NumReservedValues, NameStr);
185}
186
187LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
188 unsigned NumReservedValues, const Twine &NameStr,
189 BasicBlock *InsertAtEnd)
190 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) {
191 init(PersonalityFn, 1 + NumReservedValues, NameStr);
192}
193
194LandingPadInst::LandingPadInst(const LandingPadInst &LP)
195 : Instruction(LP.getType(), Instruction::LandingPad,
196 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
197 ReservedSpace(LP.getNumOperands()) {
198 Use *OL = OperandList, *InOL = LP.OperandList;
199 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
200 OL[I] = InOL[I];
201
202 setCleanup(LP.isCleanup());
203}
204
205LandingPadInst::~LandingPadInst() {
206 dropHungoffUses();
207}
208
209LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
210 unsigned NumReservedClauses,
211 const Twine &NameStr,
212 Instruction *InsertBefore) {
213 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
214 InsertBefore);
215}
216
217LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
218 unsigned NumReservedClauses,
219 const Twine &NameStr,
220 BasicBlock *InsertAtEnd) {
221 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
222 InsertAtEnd);
223}
224
225void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
226 const Twine &NameStr) {
227 ReservedSpace = NumReservedValues;
228 NumOperands = 1;
229 OperandList = allocHungoffUses(ReservedSpace);
230 OperandList[0] = PersFn;
231 setName(NameStr);
232 setCleanup(false);
233}
234
235/// growOperands - grow operands - This grows the operand list in response to a
236/// push_back style of operation. This grows the number of ops by 2 times.
237void LandingPadInst::growOperands(unsigned Size) {
238 unsigned e = getNumOperands();
239 if (ReservedSpace >= e + Size) return;
240 ReservedSpace = (e + Size / 2) * 2;
241
242 Use *NewOps = allocHungoffUses(ReservedSpace);
243 Use *OldOps = OperandList;
244 for (unsigned i = 0; i != e; ++i)
245 NewOps[i] = OldOps[i];
246
247 OperandList = NewOps;
248 Use::zap(OldOps, OldOps + e, true);
249}
250
251void LandingPadInst::addClause(Value *Val) {
252 unsigned OpNo = getNumOperands();
253 growOperands(1);
254 assert(OpNo < ReservedSpace && "Growing didn't work!");
255 ++NumOperands;
256 OperandList[OpNo] = Val;
257}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000258
259//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260// CallInst Implementation
261//===----------------------------------------------------------------------===//
262
Gordon Henriksen14a55692007-12-10 02:14:30 +0000263CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000264}
265
Jay Foad5bd375a2011-07-15 08:37:34 +0000266void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
267 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000268 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269
Jay Foad5bd375a2011-07-15 08:37:34 +0000270#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000271 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
273
Jay Foad5bd375a2011-07-15 08:37:34 +0000274 assert((Args.size() == FTy->getNumParams() ||
275 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000276 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000277
278 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000279 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000280 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000281 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000282#endif
283
284 std::copy(Args.begin(), Args.end(), op_begin());
285 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286}
287
Jay Foad5bd375a2011-07-15 08:37:34 +0000288void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000289 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000290 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000291
Jay Foad5bd375a2011-07-15 08:37:34 +0000292#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000293 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
295
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000296 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000297#endif
298
299 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Daniel Dunbar4975db62009-07-25 04:41:11 +0000302CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 Instruction *InsertBefore)
304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000306 Instruction::Call,
307 OperandTraits<CallInst>::op_end(this) - 1,
308 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000309 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000310}
311
Daniel Dunbar4975db62009-07-25 04:41:11 +0000312CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313 BasicBlock *InsertAtEnd)
314 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
315 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000316 Instruction::Call,
317 OperandTraits<CallInst>::op_end(this) - 1,
318 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000319 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
321
Misha Brukmanb1c93172005-04-21 23:48:37 +0000322CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000323 : Instruction(CI.getType(), Instruction::Call,
324 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000325 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000326 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000327 setTailCall(CI.isTailCall());
328 setCallingConv(CI.getCallingConv());
329
Jay Foad5bd375a2011-07-15 08:37:34 +0000330 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000331 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332}
333
Devang Patel4c758ea2008-09-25 21:00:45 +0000334void CallInst::addAttribute(unsigned i, Attributes attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000335 AttributeSet PAL = getAttributes();
Bill Wendling722b26c2012-10-14 07:35:59 +0000336 PAL = PAL.addAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000337 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000338}
339
Devang Patel4c758ea2008-09-25 21:00:45 +0000340void CallInst::removeAttribute(unsigned i, Attributes attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000341 AttributeSet PAL = getAttributes();
Bill Wendling85a64c22012-10-14 06:39:53 +0000342 PAL = PAL.removeAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000343 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000344}
345
Bill Wendlingff758fb2012-10-09 21:49:51 +0000346bool CallInst::hasFnAttr(Attributes::AttrVal A) const {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000347 if (AttributeList.getParamAttributes(AttributeSet::FunctionIndex)
Bill Wendlingfbd38fe2012-10-15 07:29:08 +0000348 .hasAttribute(A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000349 return true;
350 if (const Function *F = getCalledFunction())
Bill Wendlinge94d8432012-12-07 23:16:57 +0000351 return F->getParamAttributes(AttributeSet::FunctionIndex).hasAttribute(A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000352 return false;
353}
354
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000355bool CallInst::paramHasAttr(unsigned i, Attributes::AttrVal A) const {
356 if (AttributeList.getParamAttributes(i).hasAttribute(A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000357 return true;
358 if (const Function *F = getCalledFunction())
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000359 return F->getParamAttributes(i).hasAttribute(A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000360 return false;
361}
362
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000363/// IsConstantOne - Return true only if val is constant int 1
364static bool IsConstantOne(Value *val) {
365 assert(val && "IsConstantOne does not work with NULL val");
366 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
367}
368
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000369static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000370 BasicBlock *InsertAtEnd, Type *IntPtrTy,
371 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000372 Value *ArraySize, Function *MallocF,
373 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000374 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000375 "createMalloc needs either InsertBefore or InsertAtEnd");
376
377 // malloc(type) becomes:
378 // bitcast (i8* malloc(typeSize)) to type*
379 // malloc(type, arraySize) becomes:
380 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000381 if (!ArraySize)
382 ArraySize = ConstantInt::get(IntPtrTy, 1);
383 else if (ArraySize->getType() != IntPtrTy) {
384 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000385 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
386 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000387 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000388 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
389 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000390 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000391
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000392 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000393 if (IsConstantOne(AllocSize)) {
394 AllocSize = ArraySize; // Operand * 1 = Operand
395 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
396 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
397 false /*ZExt*/);
398 // Malloc arg is constant product of type size and array size
399 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
400 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000401 // Multiply type size by the array size...
402 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000403 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
404 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000405 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000406 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
407 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000409 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000410
Victor Hernandez788eaab2009-09-18 19:20:02 +0000411 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000412 // Create the call to Malloc.
413 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
414 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000415 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000416 Value *MallocFunc = MallocF;
417 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000418 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000419 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000420 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000421 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000422 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000423 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000424 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000425 Result = MCall;
426 if (Result->getType() != AllocPtrType)
427 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000428 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000429 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000430 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000431 Result = MCall;
432 if (Result->getType() != AllocPtrType) {
433 InsertAtEnd->getInstList().push_back(MCall);
434 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000435 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000436 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000437 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000438 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000439 if (Function *F = dyn_cast<Function>(MallocFunc)) {
440 MCall->setCallingConv(F->getCallingConv());
441 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
442 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000443 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000444
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000445 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000446}
447
448/// CreateMalloc - Generate the IR for a call to malloc:
449/// 1. Compute the malloc call's argument as the specified type's size,
450/// possibly multiplied by the array size if the array size is not
451/// constant 1.
452/// 2. Call malloc with that argument.
453/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000454Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000455 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000456 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000457 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000458 const Twine &Name) {
459 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000460 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461}
462
463/// CreateMalloc - Generate the IR for a call to malloc:
464/// 1. Compute the malloc call's argument as the specified type's size,
465/// possibly multiplied by the array size if the array size is not
466/// constant 1.
467/// 2. Call malloc with that argument.
468/// 3. Bitcast the result of the malloc call to the specified type.
469/// Note: This function does not add the bitcast to the basic block, that is the
470/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000471Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000472 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 Value *AllocSize, Value *ArraySize,
474 Function *MallocF, const Twine &Name) {
475 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000476 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000477}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000478
Victor Hernandeze2971492009-10-24 04:23:03 +0000479static Instruction* createFree(Value* Source, Instruction *InsertBefore,
480 BasicBlock *InsertAtEnd) {
481 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
482 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000483 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000484 "Can not free something of nonpointer type!");
485
486 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
487 Module* M = BB->getParent()->getParent();
488
Chris Lattner229907c2011-07-18 04:54:35 +0000489 Type *VoidTy = Type::getVoidTy(M->getContext());
490 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000491 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000492 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000493 CallInst* Result = NULL;
494 Value *PtrCast = Source;
495 if (InsertBefore) {
496 if (Source->getType() != IntPtrTy)
497 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
498 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
499 } else {
500 if (Source->getType() != IntPtrTy)
501 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
502 Result = CallInst::Create(FreeFunc, PtrCast, "");
503 }
504 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000505 if (Function *F = dyn_cast<Function>(FreeFunc))
506 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000507
508 return Result;
509}
510
511/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000512Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
513 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000514}
515
516/// CreateFree - Generate the IR for a call to the builtin free function.
517/// Note: This function does not add the call to the basic block, that is the
518/// responsibility of the caller.
519Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
520 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
521 assert(FreeCall && "CreateFree did not create a CallInst");
522 return FreeCall;
523}
524
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000525//===----------------------------------------------------------------------===//
526// InvokeInst Implementation
527//===----------------------------------------------------------------------===//
528
529void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000530 ArrayRef<Value *> Args, const Twine &NameStr) {
531 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000532 Op<-3>() = Fn;
533 Op<-2>() = IfNormal;
534 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000535
536#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000537 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000539
Jay Foad5bd375a2011-07-15 08:37:34 +0000540 assert(((Args.size() == FTy->getNumParams()) ||
541 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000542 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000543
Jay Foad5bd375a2011-07-15 08:37:34 +0000544 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000545 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000546 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000547 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000548#endif
549
550 std::copy(Args.begin(), Args.end(), op_begin());
551 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000552}
553
Misha Brukmanb1c93172005-04-21 23:48:37 +0000554InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000555 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000556 OperandTraits<InvokeInst>::op_end(this)
557 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000558 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000559 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000560 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000561 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000562 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000563}
564
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000565BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
566 return getSuccessor(idx);
567}
568unsigned InvokeInst::getNumSuccessorsV() const {
569 return getNumSuccessors();
570}
571void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
572 return setSuccessor(idx, B);
573}
574
Bill Wendlingff758fb2012-10-09 21:49:51 +0000575bool InvokeInst::hasFnAttr(Attributes::AttrVal A) const {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000576 if (AttributeList.getParamAttributes(AttributeSet::FunctionIndex).
Bill Wendlingfbd38fe2012-10-15 07:29:08 +0000577 hasAttribute(A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000578 return true;
579 if (const Function *F = getCalledFunction())
Bill Wendlinge94d8432012-12-07 23:16:57 +0000580 return F->getParamAttributes(AttributeSet::FunctionIndex).hasAttribute(A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000581 return false;
582}
583
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000584bool InvokeInst::paramHasAttr(unsigned i, Attributes::AttrVal A) const {
585 if (AttributeList.getParamAttributes(i).hasAttribute(A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000586 return true;
587 if (const Function *F = getCalledFunction())
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000588 return F->getParamAttributes(i).hasAttribute(A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000589 return false;
590}
591
Devang Patel4c758ea2008-09-25 21:00:45 +0000592void InvokeInst::addAttribute(unsigned i, Attributes attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000593 AttributeSet PAL = getAttributes();
Bill Wendling722b26c2012-10-14 07:35:59 +0000594 PAL = PAL.addAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000595 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000596}
597
Devang Patel4c758ea2008-09-25 21:00:45 +0000598void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000599 AttributeSet PAL = getAttributes();
Bill Wendling85a64c22012-10-14 06:39:53 +0000600 PAL = PAL.removeAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000601 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000602}
603
Bill Wendlingfae14752011-08-12 20:24:12 +0000604LandingPadInst *InvokeInst::getLandingPadInst() const {
605 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
606}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000607
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000608//===----------------------------------------------------------------------===//
609// ReturnInst Implementation
610//===----------------------------------------------------------------------===//
611
Chris Lattner2195fc42007-02-24 00:55:48 +0000612ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000613 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000614 OperandTraits<ReturnInst>::op_end(this) -
615 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000616 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000617 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000618 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000619 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000620}
621
Owen Anderson55f1c092009-08-13 21:58:54 +0000622ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
623 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000624 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
625 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000626 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000627 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000628}
Owen Anderson55f1c092009-08-13 21:58:54 +0000629ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
630 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000631 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
632 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000633 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000634 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000635}
Owen Anderson55f1c092009-08-13 21:58:54 +0000636ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
637 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000638 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000639}
640
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000641unsigned ReturnInst::getNumSuccessorsV() const {
642 return getNumSuccessors();
643}
644
Devang Patelae682fb2008-02-26 17:56:20 +0000645/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
646/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000648 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000649}
650
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000652 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000653}
654
Devang Patel59643e52008-02-23 00:35:18 +0000655ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000656}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000658//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000659// ResumeInst Implementation
660//===----------------------------------------------------------------------===//
661
662ResumeInst::ResumeInst(const ResumeInst &RI)
663 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
664 OperandTraits<ResumeInst>::op_begin(this), 1) {
665 Op<0>() = RI.Op<0>();
666}
667
668ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
669 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
670 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
671 Op<0>() = Exn;
672}
673
674ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
675 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
676 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
677 Op<0>() = Exn;
678}
679
680unsigned ResumeInst::getNumSuccessorsV() const {
681 return getNumSuccessors();
682}
683
684void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
685 llvm_unreachable("ResumeInst has no successors!");
686}
687
688BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
689 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000690}
691
692//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000693// UnreachableInst Implementation
694//===----------------------------------------------------------------------===//
695
Owen Anderson55f1c092009-08-13 21:58:54 +0000696UnreachableInst::UnreachableInst(LLVMContext &Context,
697 Instruction *InsertBefore)
698 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
699 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000700}
Owen Anderson55f1c092009-08-13 21:58:54 +0000701UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
702 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
703 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
705
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706unsigned UnreachableInst::getNumSuccessorsV() const {
707 return getNumSuccessors();
708}
709
710void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000711 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000712}
713
714BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000715 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000716}
717
718//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719// BranchInst Implementation
720//===----------------------------------------------------------------------===//
721
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000722void BranchInst::AssertOK() {
723 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000724 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000726}
727
Chris Lattner2195fc42007-02-24 00:55:48 +0000728BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000729 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000730 OperandTraits<BranchInst>::op_end(this) - 1,
731 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000732 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000733 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000734}
735BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
736 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000737 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000738 OperandTraits<BranchInst>::op_end(this) - 3,
739 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000740 Op<-1>() = IfTrue;
741 Op<-2>() = IfFalse;
742 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000743#ifndef NDEBUG
744 AssertOK();
745#endif
746}
747
748BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000749 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000750 OperandTraits<BranchInst>::op_end(this) - 1,
751 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000752 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000753 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000754}
755
756BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
757 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000758 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000759 OperandTraits<BranchInst>::op_end(this) - 3,
760 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000761 Op<-1>() = IfTrue;
762 Op<-2>() = IfFalse;
763 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000764#ifndef NDEBUG
765 AssertOK();
766#endif
767}
768
769
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000770BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000771 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000772 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
773 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000774 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000775 if (BI.getNumOperands() != 1) {
776 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000777 Op<-3>() = BI.Op<-3>();
778 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000779 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000780 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000781}
782
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000783void BranchInst::swapSuccessors() {
784 assert(isConditional() &&
785 "Cannot swap successors of an unconditional branch");
786 Op<-1>().swap(Op<-2>());
787
788 // Update profile metadata if present and it matches our structural
789 // expectations.
790 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
791 if (!ProfileData || ProfileData->getNumOperands() != 3)
792 return;
793
794 // The first operand is the name. Fetch them backwards and build a new one.
795 Value *Ops[] = {
796 ProfileData->getOperand(0),
797 ProfileData->getOperand(2),
798 ProfileData->getOperand(1)
799 };
800 setMetadata(LLVMContext::MD_prof,
801 MDNode::get(ProfileData->getContext(), Ops));
802}
803
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000804BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
805 return getSuccessor(idx);
806}
807unsigned BranchInst::getNumSuccessorsV() const {
808 return getNumSuccessors();
809}
810void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
811 setSuccessor(idx, B);
812}
813
814
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000815//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000816// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000817//===----------------------------------------------------------------------===//
818
Owen Andersonb6b25302009-07-14 23:09:55 +0000819static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000820 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000821 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000822 else {
823 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000824 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000825 assert(Amt->getType()->isIntegerTy() &&
826 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000827 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000828 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000829}
830
Chris Lattner229907c2011-07-18 04:54:35 +0000831AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000832 const Twine &Name, Instruction *InsertBefore)
833 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
834 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
835 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000836 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000837 setName(Name);
838}
839
Chris Lattner229907c2011-07-18 04:54:35 +0000840AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000841 const Twine &Name, BasicBlock *InsertAtEnd)
842 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
843 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
844 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000845 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000846 setName(Name);
847}
848
Chris Lattner229907c2011-07-18 04:54:35 +0000849AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000850 Instruction *InsertBefore)
851 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
852 getAISize(Ty->getContext(), 0), InsertBefore) {
853 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000854 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000855 setName(Name);
856}
857
Chris Lattner229907c2011-07-18 04:54:35 +0000858AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000859 BasicBlock *InsertAtEnd)
860 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
861 getAISize(Ty->getContext(), 0), InsertAtEnd) {
862 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000863 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000864 setName(Name);
865}
866
Chris Lattner229907c2011-07-18 04:54:35 +0000867AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000868 const Twine &Name, Instruction *InsertBefore)
869 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000870 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000871 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000872 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000873 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000874}
875
Chris Lattner229907c2011-07-18 04:54:35 +0000876AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000877 const Twine &Name, BasicBlock *InsertAtEnd)
878 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000879 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000880 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000881 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000882 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000883}
884
Gordon Henriksen14a55692007-12-10 02:14:30 +0000885// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000886AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000887}
888
Victor Hernandez8acf2952009-10-23 21:09:37 +0000889void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000890 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000891 assert(Align <= MaximumAlignment &&
892 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000893 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000894 assert(getAlignment() == Align && "Alignment representation error!");
895}
896
Victor Hernandez8acf2952009-10-23 21:09:37 +0000897bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000898 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000899 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000900 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901}
902
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000903Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904 return getType()->getElementType();
905}
906
Chris Lattner8b291e62008-11-26 02:54:17 +0000907/// isStaticAlloca - Return true if this alloca is in the entry block of the
908/// function and is a constant size. If so, the code generator will fold it
909/// into the prolog/epilog code, so it is basically free.
910bool AllocaInst::isStaticAlloca() const {
911 // Must be constant size.
912 if (!isa<ConstantInt>(getArraySize())) return false;
913
914 // Must be in the entry block.
915 const BasicBlock *Parent = getParent();
916 return Parent == &Parent->getParent()->front();
917}
918
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000919//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000920// LoadInst Implementation
921//===----------------------------------------------------------------------===//
922
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000923void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000924 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000925 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000926 assert(!(isAtomic() && getAlignment() == 0) &&
927 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000928}
929
Daniel Dunbar4975db62009-07-25 04:41:11 +0000930LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000932 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000933 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000934 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000935 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000937 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000938}
939
Daniel Dunbar4975db62009-07-25 04:41:11 +0000940LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000942 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000943 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000944 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000945 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000946 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000947 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000948}
949
Daniel Dunbar4975db62009-07-25 04:41:11 +0000950LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000951 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000952 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000953 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000954 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000955 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000956 setAtomic(NotAtomic);
957 AssertOK();
958 setName(Name);
959}
960
961LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
962 BasicBlock *InsertAE)
963 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
964 Load, Ptr, InsertAE) {
965 setVolatile(isVolatile);
966 setAlignment(0);
967 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +0000968 AssertOK();
969 setName(Name);
970}
971
Daniel Dunbar4975db62009-07-25 04:41:11 +0000972LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000973 unsigned Align, Instruction *InsertBef)
974 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
975 Load, Ptr, InsertBef) {
976 setVolatile(isVolatile);
977 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000978 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000979 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000980 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000981}
982
Daniel Dunbar4975db62009-07-25 04:41:11 +0000983LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000984 unsigned Align, BasicBlock *InsertAE)
985 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
986 Load, Ptr, InsertAE) {
987 setVolatile(isVolatile);
988 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000989 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +0000990 AssertOK();
991 setName(Name);
992}
993
Eli Friedman59b66882011-08-09 23:02:53 +0000994LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
995 unsigned Align, AtomicOrdering Order,
996 SynchronizationScope SynchScope,
997 Instruction *InsertBef)
998 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
999 Load, Ptr, InsertBef) {
1000 setVolatile(isVolatile);
1001 setAlignment(Align);
1002 setAtomic(Order, SynchScope);
1003 AssertOK();
1004 setName(Name);
1005}
1006
1007LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1008 unsigned Align, AtomicOrdering Order,
1009 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001010 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001011 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001012 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001013 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001014 setAlignment(Align);
1015 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001016 AssertOK();
1017 setName(Name);
1018}
1019
Daniel Dunbar27096822009-08-11 18:11:15 +00001020LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1021 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1022 Load, Ptr, InsertBef) {
1023 setVolatile(false);
1024 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001025 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001026 AssertOK();
1027 if (Name && Name[0]) setName(Name);
1028}
1029
1030LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1031 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1032 Load, Ptr, InsertAE) {
1033 setVolatile(false);
1034 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001035 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001036 AssertOK();
1037 if (Name && Name[0]) setName(Name);
1038}
1039
1040LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1041 Instruction *InsertBef)
1042: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1043 Load, Ptr, InsertBef) {
1044 setVolatile(isVolatile);
1045 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001046 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001047 AssertOK();
1048 if (Name && Name[0]) setName(Name);
1049}
1050
1051LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1052 BasicBlock *InsertAE)
1053 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1054 Load, Ptr, InsertAE) {
1055 setVolatile(isVolatile);
1056 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001057 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001058 AssertOK();
1059 if (Name && Name[0]) setName(Name);
1060}
1061
Christopher Lamb84485702007-04-22 19:24:39 +00001062void LoadInst::setAlignment(unsigned Align) {
1063 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001064 assert(Align <= MaximumAlignment &&
1065 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001066 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001067 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001068 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001069}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001070
1071//===----------------------------------------------------------------------===//
1072// StoreInst Implementation
1073//===----------------------------------------------------------------------===//
1074
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001075void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001076 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001077 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001078 "Ptr must have pointer type!");
1079 assert(getOperand(0)->getType() ==
1080 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001081 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001082 assert(!(isAtomic() && getAlignment() == 0) &&
1083 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001084}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001085
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001086
1087StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001088 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001089 OperandTraits<StoreInst>::op_begin(this),
1090 OperandTraits<StoreInst>::operands(this),
1091 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001092 Op<0>() = val;
1093 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001094 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001095 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001096 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001097 AssertOK();
1098}
1099
1100StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001101 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001102 OperandTraits<StoreInst>::op_begin(this),
1103 OperandTraits<StoreInst>::operands(this),
1104 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001105 Op<0>() = val;
1106 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001107 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001108 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001109 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001110 AssertOK();
1111}
1112
Misha Brukmanb1c93172005-04-21 23:48:37 +00001113StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001114 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001115 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001116 OperandTraits<StoreInst>::op_begin(this),
1117 OperandTraits<StoreInst>::operands(this),
1118 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001119 Op<0>() = val;
1120 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001121 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001122 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001123 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001124 AssertOK();
1125}
1126
1127StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1128 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001129 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001130 OperandTraits<StoreInst>::op_begin(this),
1131 OperandTraits<StoreInst>::operands(this),
1132 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001133 Op<0>() = val;
1134 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001135 setVolatile(isVolatile);
1136 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001137 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001138 AssertOK();
1139}
1140
Misha Brukmanb1c93172005-04-21 23:48:37 +00001141StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001142 unsigned Align, AtomicOrdering Order,
1143 SynchronizationScope SynchScope,
1144 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001145 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001146 OperandTraits<StoreInst>::op_begin(this),
1147 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001148 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001149 Op<0>() = val;
1150 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001151 setVolatile(isVolatile);
1152 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001153 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001154 AssertOK();
1155}
1156
1157StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001158 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001159 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001160 OperandTraits<StoreInst>::op_begin(this),
1161 OperandTraits<StoreInst>::operands(this),
1162 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001163 Op<0>() = val;
1164 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001165 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001166 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001167 setAtomic(NotAtomic);
1168 AssertOK();
1169}
1170
1171StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1172 unsigned Align, BasicBlock *InsertAtEnd)
1173 : Instruction(Type::getVoidTy(val->getContext()), Store,
1174 OperandTraits<StoreInst>::op_begin(this),
1175 OperandTraits<StoreInst>::operands(this),
1176 InsertAtEnd) {
1177 Op<0>() = val;
1178 Op<1>() = addr;
1179 setVolatile(isVolatile);
1180 setAlignment(Align);
1181 setAtomic(NotAtomic);
1182 AssertOK();
1183}
1184
1185StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1186 unsigned Align, AtomicOrdering Order,
1187 SynchronizationScope SynchScope,
1188 BasicBlock *InsertAtEnd)
1189 : Instruction(Type::getVoidTy(val->getContext()), Store,
1190 OperandTraits<StoreInst>::op_begin(this),
1191 OperandTraits<StoreInst>::operands(this),
1192 InsertAtEnd) {
1193 Op<0>() = val;
1194 Op<1>() = addr;
1195 setVolatile(isVolatile);
1196 setAlignment(Align);
1197 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001198 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001199}
1200
Christopher Lamb84485702007-04-22 19:24:39 +00001201void StoreInst::setAlignment(unsigned Align) {
1202 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001203 assert(Align <= MaximumAlignment &&
1204 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001205 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001206 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001207 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001208}
1209
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001210//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001211// AtomicCmpXchgInst Implementation
1212//===----------------------------------------------------------------------===//
1213
1214void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1215 AtomicOrdering Ordering,
1216 SynchronizationScope SynchScope) {
1217 Op<0>() = Ptr;
1218 Op<1>() = Cmp;
1219 Op<2>() = NewVal;
1220 setOrdering(Ordering);
1221 setSynchScope(SynchScope);
1222
1223 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1224 "All operands must be non-null!");
1225 assert(getOperand(0)->getType()->isPointerTy() &&
1226 "Ptr must have pointer type!");
1227 assert(getOperand(1)->getType() ==
1228 cast<PointerType>(getOperand(0)->getType())->getElementType()
1229 && "Ptr must be a pointer to Cmp type!");
1230 assert(getOperand(2)->getType() ==
1231 cast<PointerType>(getOperand(0)->getType())->getElementType()
1232 && "Ptr must be a pointer to NewVal type!");
1233 assert(Ordering != NotAtomic &&
1234 "AtomicCmpXchg instructions must be atomic!");
1235}
1236
1237AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1238 AtomicOrdering Ordering,
1239 SynchronizationScope SynchScope,
1240 Instruction *InsertBefore)
1241 : Instruction(Cmp->getType(), AtomicCmpXchg,
1242 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1243 OperandTraits<AtomicCmpXchgInst>::operands(this),
1244 InsertBefore) {
1245 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1246}
1247
1248AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1249 AtomicOrdering Ordering,
1250 SynchronizationScope SynchScope,
1251 BasicBlock *InsertAtEnd)
1252 : Instruction(Cmp->getType(), AtomicCmpXchg,
1253 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1254 OperandTraits<AtomicCmpXchgInst>::operands(this),
1255 InsertAtEnd) {
1256 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1257}
1258
1259//===----------------------------------------------------------------------===//
1260// AtomicRMWInst Implementation
1261//===----------------------------------------------------------------------===//
1262
1263void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1264 AtomicOrdering Ordering,
1265 SynchronizationScope SynchScope) {
1266 Op<0>() = Ptr;
1267 Op<1>() = Val;
1268 setOperation(Operation);
1269 setOrdering(Ordering);
1270 setSynchScope(SynchScope);
1271
1272 assert(getOperand(0) && getOperand(1) &&
1273 "All operands must be non-null!");
1274 assert(getOperand(0)->getType()->isPointerTy() &&
1275 "Ptr must have pointer type!");
1276 assert(getOperand(1)->getType() ==
1277 cast<PointerType>(getOperand(0)->getType())->getElementType()
1278 && "Ptr must be a pointer to Val type!");
1279 assert(Ordering != NotAtomic &&
1280 "AtomicRMW instructions must be atomic!");
1281}
1282
1283AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1284 AtomicOrdering Ordering,
1285 SynchronizationScope SynchScope,
1286 Instruction *InsertBefore)
1287 : Instruction(Val->getType(), AtomicRMW,
1288 OperandTraits<AtomicRMWInst>::op_begin(this),
1289 OperandTraits<AtomicRMWInst>::operands(this),
1290 InsertBefore) {
1291 Init(Operation, Ptr, Val, Ordering, SynchScope);
1292}
1293
1294AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1295 AtomicOrdering Ordering,
1296 SynchronizationScope SynchScope,
1297 BasicBlock *InsertAtEnd)
1298 : Instruction(Val->getType(), AtomicRMW,
1299 OperandTraits<AtomicRMWInst>::op_begin(this),
1300 OperandTraits<AtomicRMWInst>::operands(this),
1301 InsertAtEnd) {
1302 Init(Operation, Ptr, Val, Ordering, SynchScope);
1303}
1304
1305//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001306// FenceInst Implementation
1307//===----------------------------------------------------------------------===//
1308
1309FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1310 SynchronizationScope SynchScope,
1311 Instruction *InsertBefore)
1312 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1313 setOrdering(Ordering);
1314 setSynchScope(SynchScope);
1315}
1316
1317FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1318 SynchronizationScope SynchScope,
1319 BasicBlock *InsertAtEnd)
1320 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1321 setOrdering(Ordering);
1322 setSynchScope(SynchScope);
1323}
1324
1325//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001326// GetElementPtrInst Implementation
1327//===----------------------------------------------------------------------===//
1328
Jay Foadd1b78492011-07-25 09:48:08 +00001329void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001330 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001331 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1332 OperandList[0] = Ptr;
1333 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001334 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001335}
1336
Gabor Greiff6caff662008-05-10 08:32:32 +00001337GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001338 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001339 OperandTraits<GetElementPtrInst>::op_end(this)
1340 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001341 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001342 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001343 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001344}
1345
Chris Lattner6090a422009-03-09 04:46:40 +00001346/// getIndexedType - Returns the type of the element that would be accessed with
1347/// a gep instruction with the specified parameters.
1348///
1349/// The Idxs pointer should point to a continuous piece of memory containing the
1350/// indices, either as Value* or uint64_t.
1351///
1352/// A null type is returned if the indices are invalid for the specified
1353/// pointer type.
1354///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001355template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001356static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Duncan Sandse6beec62012-11-13 12:59:33 +00001357 PointerType *PTy = dyn_cast<PointerType>(Ptr->getScalarType());
Dan Gohman12fce772008-05-15 19:50:34 +00001358 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001359 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001360
Chris Lattner6090a422009-03-09 04:46:40 +00001361 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001362 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001363 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001364
Chris Lattner6090a422009-03-09 04:46:40 +00001365 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001366 // it cannot be 'stepped over'.
1367 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001368 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001369
Dan Gohman1ecaf452008-05-31 00:58:22 +00001370 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001371 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001372 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001373 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001374 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001375 if (!CT->indexValid(Index)) return 0;
1376 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001377 }
Jay Foadd1b78492011-07-25 09:48:08 +00001378 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001379}
1380
Jay Foadd1b78492011-07-25 09:48:08 +00001381Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1382 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001383}
1384
Chris Lattner229907c2011-07-18 04:54:35 +00001385Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001386 ArrayRef<Constant *> IdxList) {
1387 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001388}
1389
Jay Foadd1b78492011-07-25 09:48:08 +00001390Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1391 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001392}
1393
Chris Lattner45f15572007-04-14 00:12:57 +00001394/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1395/// zeros. If so, the result pointer and the first operand have the same
1396/// value, just potentially different types.
1397bool GetElementPtrInst::hasAllZeroIndices() const {
1398 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1399 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1400 if (!CI->isZero()) return false;
1401 } else {
1402 return false;
1403 }
1404 }
1405 return true;
1406}
1407
Chris Lattner27058292007-04-27 20:35:56 +00001408/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1409/// constant integers. If so, the result pointer and the first operand have
1410/// a constant offset between them.
1411bool GetElementPtrInst::hasAllConstantIndices() const {
1412 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1413 if (!isa<ConstantInt>(getOperand(i)))
1414 return false;
1415 }
1416 return true;
1417}
1418
Dan Gohman1b849082009-09-07 23:54:19 +00001419void GetElementPtrInst::setIsInBounds(bool B) {
1420 cast<GEPOperator>(this)->setIsInBounds(B);
1421}
Chris Lattner45f15572007-04-14 00:12:57 +00001422
Nick Lewycky28a5f252009-09-27 21:33:04 +00001423bool GetElementPtrInst::isInBounds() const {
1424 return cast<GEPOperator>(this)->isInBounds();
1425}
1426
Chandler Carruth1e140532012-12-11 10:29:10 +00001427bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1428 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001429 // Delegate to the generic GEPOperator implementation.
1430 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001431}
1432
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001433//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001434// ExtractElementInst Implementation
1435//===----------------------------------------------------------------------===//
1436
1437ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001438 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001439 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001440 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001441 ExtractElement,
1442 OperandTraits<ExtractElementInst>::op_begin(this),
1443 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001444 assert(isValidOperands(Val, Index) &&
1445 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001446 Op<0>() = Val;
1447 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001448 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001449}
1450
1451ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001452 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001453 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001454 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001455 ExtractElement,
1456 OperandTraits<ExtractElementInst>::op_begin(this),
1457 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001458 assert(isValidOperands(Val, Index) &&
1459 "Invalid extractelement instruction operands!");
1460
Gabor Greif2d3024d2008-05-26 21:33:52 +00001461 Op<0>() = Val;
1462 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001463 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001464}
1465
Chris Lattner65511ff2006-10-05 06:24:58 +00001466
Chris Lattner54865b32006-04-08 04:05:48 +00001467bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001468 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001469 return false;
1470 return true;
1471}
1472
1473
Robert Bocchino23004482006-01-10 19:05:34 +00001474//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001475// InsertElementInst Implementation
1476//===----------------------------------------------------------------------===//
1477
Chris Lattner54865b32006-04-08 04:05:48 +00001478InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001479 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001480 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001481 : Instruction(Vec->getType(), InsertElement,
1482 OperandTraits<InsertElementInst>::op_begin(this),
1483 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001484 assert(isValidOperands(Vec, Elt, Index) &&
1485 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001486 Op<0>() = Vec;
1487 Op<1>() = Elt;
1488 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001489 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001490}
1491
Chris Lattner54865b32006-04-08 04:05:48 +00001492InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001493 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001494 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001495 : Instruction(Vec->getType(), InsertElement,
1496 OperandTraits<InsertElementInst>::op_begin(this),
1497 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001498 assert(isValidOperands(Vec, Elt, Index) &&
1499 "Invalid insertelement instruction operands!");
1500
Gabor Greif2d3024d2008-05-26 21:33:52 +00001501 Op<0>() = Vec;
1502 Op<1>() = Elt;
1503 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001504 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001505}
1506
Chris Lattner54865b32006-04-08 04:05:48 +00001507bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1508 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001509 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001510 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001511
Reid Spencerd84d35b2007-02-15 02:26:10 +00001512 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001513 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001514
Duncan Sands9dff9be2010-02-15 16:12:20 +00001515 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001516 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001517 return true;
1518}
1519
1520
Robert Bocchinoca27f032006-01-17 20:07:22 +00001521//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001522// ShuffleVectorInst Implementation
1523//===----------------------------------------------------------------------===//
1524
1525ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001526 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001527 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001528: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001529 cast<VectorType>(Mask->getType())->getNumElements()),
1530 ShuffleVector,
1531 OperandTraits<ShuffleVectorInst>::op_begin(this),
1532 OperandTraits<ShuffleVectorInst>::operands(this),
1533 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001534 assert(isValidOperands(V1, V2, Mask) &&
1535 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001536 Op<0>() = V1;
1537 Op<1>() = V2;
1538 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001539 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001540}
1541
1542ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001543 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001544 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001545: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1546 cast<VectorType>(Mask->getType())->getNumElements()),
1547 ShuffleVector,
1548 OperandTraits<ShuffleVectorInst>::op_begin(this),
1549 OperandTraits<ShuffleVectorInst>::operands(this),
1550 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001551 assert(isValidOperands(V1, V2, Mask) &&
1552 "Invalid shuffle vector instruction operands!");
1553
Gabor Greif2d3024d2008-05-26 21:33:52 +00001554 Op<0>() = V1;
1555 Op<1>() = V2;
1556 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001557 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001558}
1559
Mon P Wang25f01062008-11-10 04:46:22 +00001560bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001561 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001562 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001563 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001564 return false;
1565
Chris Lattner1dcb6542012-01-25 23:49:49 +00001566 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001567 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001568 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001569 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001570
1571 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001572 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1573 return true;
1574
Nate Begeman60a31c32010-08-13 00:16:46 +00001575 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001576 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001577 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001578 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1579 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001580 return false;
1581 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1582 return false;
1583 }
1584 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001585 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001586 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001587
1588 if (const ConstantDataSequential *CDS =
1589 dyn_cast<ConstantDataSequential>(Mask)) {
1590 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1591 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1592 if (CDS->getElementAsInteger(i) >= V1Size*2)
1593 return false;
1594 return true;
1595 }
1596
1597 // The bitcode reader can create a place holder for a forward reference
1598 // used as the shuffle mask. When this occurs, the shuffle mask will
1599 // fall into this case and fail. To avoid this error, do this bit of
1600 // ugliness to allow such a mask pass.
1601 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1602 if (CE->getOpcode() == Instruction::UserOp1)
1603 return true;
1604
1605 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001606}
1607
Chris Lattnerf724e342008-03-02 05:28:33 +00001608/// getMaskValue - Return the index from the shuffle mask for the specified
1609/// output result. This is either -1 if the element is undef or a number less
1610/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001611int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1612 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1613 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001614 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001615 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001616 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001617 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001618 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001619}
1620
Chris Lattner1dcb6542012-01-25 23:49:49 +00001621/// getShuffleMask - Return the full mask for this instruction, where each
1622/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001623void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1624 SmallVectorImpl<int> &Result) {
1625 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001626
Chris Lattnercf129702012-01-26 02:51:13 +00001627 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001628 for (unsigned i = 0; i != NumElts; ++i)
1629 Result.push_back(CDS->getElementAsInteger(i));
1630 return;
1631 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001632 for (unsigned i = 0; i != NumElts; ++i) {
1633 Constant *C = Mask->getAggregateElement(i);
1634 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001635 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001636 }
1637}
1638
1639
Dan Gohman12fce772008-05-15 19:50:34 +00001640//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001641// InsertValueInst Class
1642//===----------------------------------------------------------------------===//
1643
Jay Foad57aa6362011-07-13 10:26:04 +00001644void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1645 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001646 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001647
1648 // There's no fundamental reason why we require at least one index
1649 // (other than weirdness with &*IdxBegin being invalid; see
1650 // getelementptr's init routine for example). But there's no
1651 // present need to support it.
1652 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1653
1654 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001655 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001656 Op<0>() = Agg;
1657 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001658
Jay Foad57aa6362011-07-13 10:26:04 +00001659 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001660 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001661}
1662
1663InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001664 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001665 OperandTraits<InsertValueInst>::op_begin(this), 2),
1666 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001667 Op<0>() = IVI.getOperand(0);
1668 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001669 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001670}
1671
1672//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001673// ExtractValueInst Class
1674//===----------------------------------------------------------------------===//
1675
Jay Foad57aa6362011-07-13 10:26:04 +00001676void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001677 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001678
Jay Foad57aa6362011-07-13 10:26:04 +00001679 // There's no fundamental reason why we require at least one index.
1680 // But there's no present need to support it.
1681 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001682
Jay Foad57aa6362011-07-13 10:26:04 +00001683 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001684 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001685}
1686
1687ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001688 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001689 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001690 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001691}
1692
Dan Gohman12fce772008-05-15 19:50:34 +00001693// getIndexedType - Returns the type of the element that would be extracted
1694// with an extractvalue instruction with the specified parameters.
1695//
1696// A null type is returned if the indices are invalid for the specified
1697// pointer type.
1698//
Chris Lattner229907c2011-07-18 04:54:35 +00001699Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001700 ArrayRef<unsigned> Idxs) {
1701 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001702 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001703 // We can't use CompositeType::indexValid(Index) here.
1704 // indexValid() always returns true for arrays because getelementptr allows
1705 // out-of-bounds indices. Since we don't allow those for extractvalue and
1706 // insertvalue we need to check array indexing manually.
1707 // Since the only other types we can index into are struct types it's just
1708 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001709 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001710 if (Index >= AT->getNumElements())
1711 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001712 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001713 if (Index >= ST->getNumElements())
1714 return 0;
1715 } else {
1716 // Not a valid type to index into.
1717 return 0;
1718 }
1719
1720 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001721 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001722 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001723}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001724
1725//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001726// BinaryOperator Class
1727//===----------------------------------------------------------------------===//
1728
Chris Lattner2195fc42007-02-24 00:55:48 +00001729BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001730 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001731 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001732 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001733 OperandTraits<BinaryOperator>::op_begin(this),
1734 OperandTraits<BinaryOperator>::operands(this),
1735 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001736 Op<0>() = S1;
1737 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001738 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001739 setName(Name);
1740}
1741
1742BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001743 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001744 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001745 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001746 OperandTraits<BinaryOperator>::op_begin(this),
1747 OperandTraits<BinaryOperator>::operands(this),
1748 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001749 Op<0>() = S1;
1750 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001751 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001752 setName(Name);
1753}
1754
1755
1756void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001757 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001758 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001759 assert(LHS->getType() == RHS->getType() &&
1760 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001761#ifndef NDEBUG
1762 switch (iType) {
1763 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001764 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001765 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001766 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001767 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001768 "Tried to create an integer operation on a non-integer type!");
1769 break;
1770 case FAdd: case FSub:
1771 case FMul:
1772 assert(getType() == LHS->getType() &&
1773 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001774 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001775 "Tried to create a floating-point operation on a "
1776 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001777 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001778 case UDiv:
1779 case SDiv:
1780 assert(getType() == LHS->getType() &&
1781 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001782 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001783 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001784 "Incorrect operand type (not integer) for S/UDIV");
1785 break;
1786 case FDiv:
1787 assert(getType() == LHS->getType() &&
1788 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001789 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001790 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001791 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001792 case URem:
1793 case SRem:
1794 assert(getType() == LHS->getType() &&
1795 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001796 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001797 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001798 "Incorrect operand type (not integer) for S/UREM");
1799 break;
1800 case FRem:
1801 assert(getType() == LHS->getType() &&
1802 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001803 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001804 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001805 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001806 case Shl:
1807 case LShr:
1808 case AShr:
1809 assert(getType() == LHS->getType() &&
1810 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001811 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001812 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001813 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001814 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001815 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001816 case And: case Or:
1817 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001818 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001819 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001820 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001821 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001822 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001823 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001824 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001825 default:
1826 break;
1827 }
1828#endif
1829}
1830
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001831BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001832 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001833 Instruction *InsertBefore) {
1834 assert(S1->getType() == S2->getType() &&
1835 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001836 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001837}
1838
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001839BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001840 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001841 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001842 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001843 InsertAtEnd->getInstList().push_back(Res);
1844 return Res;
1845}
1846
Dan Gohman5476cfd2009-08-12 16:23:25 +00001847BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001848 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001849 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001850 return new BinaryOperator(Instruction::Sub,
1851 zero, Op,
1852 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001853}
1854
Dan Gohman5476cfd2009-08-12 16:23:25 +00001855BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001856 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001857 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001858 return new BinaryOperator(Instruction::Sub,
1859 zero, Op,
1860 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001861}
1862
Dan Gohman4ab44202009-12-18 02:58:50 +00001863BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1864 Instruction *InsertBefore) {
1865 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1866 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1867}
1868
1869BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1870 BasicBlock *InsertAtEnd) {
1871 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1872 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1873}
1874
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001875BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1876 Instruction *InsertBefore) {
1877 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1878 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1879}
1880
1881BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1882 BasicBlock *InsertAtEnd) {
1883 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1884 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1885}
1886
Dan Gohman5476cfd2009-08-12 16:23:25 +00001887BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001888 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001889 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001890 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001891 Op->getType(), Name, InsertBefore);
1892}
1893
Dan Gohman5476cfd2009-08-12 16:23:25 +00001894BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001895 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001896 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001897 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001898 Op->getType(), Name, InsertAtEnd);
1899}
1900
Dan Gohman5476cfd2009-08-12 16:23:25 +00001901BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001902 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001903 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001904 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001905 Op->getType(), Name, InsertBefore);
1906}
1907
Dan Gohman5476cfd2009-08-12 16:23:25 +00001908BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001909 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001910 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001911 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001912 Op->getType(), Name, InsertAtEnd);
1913}
1914
1915
1916// isConstantAllOnes - Helper function for several functions below
1917static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001918 if (const Constant *C = dyn_cast<Constant>(V))
1919 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001920 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001921}
1922
Owen Andersonbb2501b2009-07-13 22:18:28 +00001923bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001924 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1925 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001926 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1927 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001928 return false;
1929}
1930
Owen Andersonbb2501b2009-07-13 22:18:28 +00001931bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001932 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1933 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001934 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001935 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001936 return false;
1937}
1938
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001939bool BinaryOperator::isNot(const Value *V) {
1940 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1941 return (Bop->getOpcode() == Instruction::Xor &&
1942 (isConstantAllOnes(Bop->getOperand(1)) ||
1943 isConstantAllOnes(Bop->getOperand(0))));
1944 return false;
1945}
1946
Chris Lattner2c7d1772005-04-24 07:28:37 +00001947Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001948 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001949}
1950
Chris Lattner2c7d1772005-04-24 07:28:37 +00001951const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1952 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001953}
1954
Dan Gohmana5b96452009-06-04 22:49:04 +00001955Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001956 return cast<BinaryOperator>(BinOp)->getOperand(1);
1957}
1958
1959const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1960 return getFNegArgument(const_cast<Value*>(BinOp));
1961}
1962
Chris Lattner2c7d1772005-04-24 07:28:37 +00001963Value *BinaryOperator::getNotArgument(Value *BinOp) {
1964 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1965 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1966 Value *Op0 = BO->getOperand(0);
1967 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001968 if (isConstantAllOnes(Op0)) return Op1;
1969
1970 assert(isConstantAllOnes(Op1));
1971 return Op0;
1972}
1973
Chris Lattner2c7d1772005-04-24 07:28:37 +00001974const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1975 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001976}
1977
1978
1979// swapOperands - Exchange the two operands to this instruction. This
1980// instruction is safe to use on any binary instruction and does not
1981// modify the semantics of the instruction. If the instruction is
1982// order dependent (SetLT f.e.) the opcode is changed.
1983//
1984bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001985 if (!isCommutative())
1986 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001987 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001988 return false;
1989}
1990
Dan Gohman1b849082009-09-07 23:54:19 +00001991void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1992 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1993}
1994
1995void BinaryOperator::setHasNoSignedWrap(bool b) {
1996 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1997}
1998
1999void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002000 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002001}
2002
Nick Lewycky28a5f252009-09-27 21:33:04 +00002003bool BinaryOperator::hasNoUnsignedWrap() const {
2004 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2005}
2006
2007bool BinaryOperator::hasNoSignedWrap() const {
2008 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2009}
2010
2011bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002012 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002013}
2014
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002015//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002016// FPMathOperator Class
2017//===----------------------------------------------------------------------===//
2018
2019/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2020/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002021/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002022float FPMathOperator::getFPAccuracy() const {
2023 const MDNode *MD =
2024 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2025 if (!MD)
2026 return 0.0;
Duncan Sands9af62982012-04-16 19:39:33 +00002027 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2028 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002029}
2030
2031
2032//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002033// CastInst Class
2034//===----------------------------------------------------------------------===//
2035
David Blaikie3a15e142011-12-01 08:00:17 +00002036void CastInst::anchor() {}
2037
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038// Just determine if this cast only deals with integral->integral conversion.
2039bool CastInst::isIntegerCast() const {
2040 switch (getOpcode()) {
2041 default: return false;
2042 case Instruction::ZExt:
2043 case Instruction::SExt:
2044 case Instruction::Trunc:
2045 return true;
2046 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002047 return getOperand(0)->getType()->isIntegerTy() &&
2048 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002049 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002050}
2051
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002052bool CastInst::isLosslessCast() const {
2053 // Only BitCast can be lossless, exit fast if we're not BitCast
2054 if (getOpcode() != Instruction::BitCast)
2055 return false;
2056
2057 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002058 Type* SrcTy = getOperand(0)->getType();
2059 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060 if (SrcTy == DstTy)
2061 return true;
2062
Reid Spencer8d9336d2006-12-31 05:26:44 +00002063 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002064 if (SrcTy->isPointerTy())
2065 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002066 return false; // Other types have no identity values
2067}
2068
2069/// This function determines if the CastInst does not require any bits to be
2070/// changed in order to effect the cast. Essentially, it identifies cases where
2071/// no code gen is necessary for the cast, hence the name no-op cast. For
2072/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002073/// # bitcast i32* %x to i8*
2074/// # bitcast <2 x i32> %x to <4 x i16>
2075/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002076/// @brief Determine if the described cast is a no-op.
2077bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002078 Type *SrcTy,
2079 Type *DestTy,
2080 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002081 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002082 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002083 case Instruction::Trunc:
2084 case Instruction::ZExt:
2085 case Instruction::SExt:
2086 case Instruction::FPTrunc:
2087 case Instruction::FPExt:
2088 case Instruction::UIToFP:
2089 case Instruction::SIToFP:
2090 case Instruction::FPToUI:
2091 case Instruction::FPToSI:
2092 return false; // These always modify bits
2093 case Instruction::BitCast:
2094 return true; // BitCast never modifies bits.
2095 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002096 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002097 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002098 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002099 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002100 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002101 }
2102}
2103
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002104/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002105bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002106 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2107}
2108
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002109/// This function determines if a pair of casts can be eliminated and what
2110/// opcode should be used in the elimination. This assumes that there are two
2111/// instructions like this:
2112/// * %F = firstOpcode SrcTy %x to MidTy
2113/// * %S = secondOpcode MidTy %F to DstTy
2114/// The function returns a resultOpcode so these two casts can be replaced with:
2115/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2116/// If no such cast is permited, the function returns 0.
2117unsigned CastInst::isEliminableCastPair(
2118 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002119 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2120 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121 // Define the 144 possibilities for these two cast instructions. The values
2122 // in this matrix determine what to do in a given situation and select the
2123 // case in the switch below. The rows correspond to firstOp, the columns
2124 // correspond to secondOp. In looking at the table below, keep in mind
2125 // the following cast properties:
2126 //
2127 // Size Compare Source Destination
2128 // Operator Src ? Size Type Sign Type Sign
2129 // -------- ------------ ------------------- ---------------------
2130 // TRUNC > Integer Any Integral Any
2131 // ZEXT < Integral Unsigned Integer Any
2132 // SEXT < Integral Signed Integer Any
2133 // FPTOUI n/a FloatPt n/a Integral Unsigned
2134 // FPTOSI n/a FloatPt n/a Integral Signed
2135 // UITOFP n/a Integral Unsigned FloatPt n/a
2136 // SITOFP n/a Integral Signed FloatPt n/a
2137 // FPTRUNC > FloatPt n/a FloatPt n/a
2138 // FPEXT < FloatPt n/a FloatPt n/a
2139 // PTRTOINT n/a Pointer n/a Integral Unsigned
2140 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002141 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002142 //
2143 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002144 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2145 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002146 // of the produced value (we no longer know the top-part is all zeros).
2147 // Further this conversion is often much more expensive for typical hardware,
2148 // and causes issues when building libgcc. We disallow fptosi+sext for the
2149 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002150 const unsigned numCastOps =
2151 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2152 static const uint8_t CastResults[numCastOps][numCastOps] = {
2153 // T F F U S F F P I B -+
2154 // R Z S P P I I T P 2 N T |
2155 // U E E 2 2 2 2 R E I T C +- secondOp
2156 // N X X U S F F N X N 2 V |
2157 // C T T I I P P C T T P T -+
2158 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2159 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2160 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002161 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2162 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002163 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2164 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2165 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2166 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2167 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2168 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2169 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2170 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002171
2172 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002173 // merging. However, bitcast of A->B->A are allowed.
2174 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2175 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2176 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2177
2178 // Check if any of the bitcasts convert scalars<->vectors.
2179 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2180 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2181 // Unless we are bitcasing to the original type, disallow optimizations.
2182 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183
2184 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2185 [secondOp-Instruction::CastOpsBegin];
2186 switch (ElimCase) {
2187 case 0:
2188 // categorically disallowed
2189 return 0;
2190 case 1:
2191 // allowed, use first cast's opcode
2192 return firstOp;
2193 case 2:
2194 // allowed, use second cast's opcode
2195 return secondOp;
2196 case 3:
2197 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002198 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002199 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002200 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201 return firstOp;
2202 return 0;
2203 case 4:
2204 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002205 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002206 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002207 return firstOp;
2208 return 0;
2209 case 5:
2210 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002211 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002212 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213 return secondOp;
2214 return 0;
2215 case 6:
2216 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002217 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002218 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219 return secondOp;
2220 return 0;
2221 case 7: {
2222 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Duncan Sandse2395dc2012-10-30 16:03:32 +00002223 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002224 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002225 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002226 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002227 if (MidSize >= PtrSize)
2228 return Instruction::BitCast;
2229 return 0;
2230 }
2231 case 8: {
2232 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2233 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2234 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002235 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2236 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 if (SrcSize == DstSize)
2238 return Instruction::BitCast;
2239 else if (SrcSize < DstSize)
2240 return firstOp;
2241 return secondOp;
2242 }
2243 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2244 return Instruction::ZExt;
2245 case 10:
2246 // fpext followed by ftrunc is allowed if the bit size returned to is
2247 // the same as the original, in which case its just a bitcast
2248 if (SrcTy == DstTy)
2249 return Instruction::BitCast;
2250 return 0; // If the types are not the same we can't eliminate it.
2251 case 11:
2252 // bitcast followed by ptrtoint is allowed as long as the bitcast
2253 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002254 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002255 return secondOp;
2256 return 0;
2257 case 12:
2258 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002259 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002260 return firstOp;
2261 return 0;
2262 case 13: {
2263 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002264 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002265 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002266 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002267 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2268 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002269 if (SrcSize <= PtrSize && SrcSize == DstSize)
2270 return Instruction::BitCast;
2271 return 0;
2272 }
2273 case 99:
2274 // cast combination can't happen (error in input). This is for all cases
2275 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002276 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002277 default:
Craig Topperc514b542012-02-05 22:14:15 +00002278 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280}
2281
Chris Lattner229907c2011-07-18 04:54:35 +00002282CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002283 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002284 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002285 // Construct and return the appropriate CastInst subclass
2286 switch (op) {
2287 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2288 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2289 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2290 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2291 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2292 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2293 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2294 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2295 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2296 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2297 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2298 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002299 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002300 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301}
2302
Chris Lattner229907c2011-07-18 04:54:35 +00002303CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002304 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002305 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 // Construct and return the appropriate CastInst subclass
2307 switch (op) {
2308 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2309 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2310 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2311 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2312 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2313 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2314 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2315 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2316 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2317 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2318 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2319 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002320 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002321 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322}
2323
Chris Lattner229907c2011-07-18 04:54:35 +00002324CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002325 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002326 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002327 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002328 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2329 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002330}
2331
Chris Lattner229907c2011-07-18 04:54:35 +00002332CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002333 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002334 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002335 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002336 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2337 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002338}
2339
Chris Lattner229907c2011-07-18 04:54:35 +00002340CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002341 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002342 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002343 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002344 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2345 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002346}
2347
Chris Lattner229907c2011-07-18 04:54:35 +00002348CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002349 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002350 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002351 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002352 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2353 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002354}
2355
Chris Lattner229907c2011-07-18 04:54:35 +00002356CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002357 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002358 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002359 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002360 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2361 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002362}
2363
Chris Lattner229907c2011-07-18 04:54:35 +00002364CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002365 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002366 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002367 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002368 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2369 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002370}
2371
Chris Lattner229907c2011-07-18 04:54:35 +00002372CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002373 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002374 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002375 assert(S->getType()->isPointerTy() && "Invalid cast");
2376 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002377 "Invalid cast");
2378
Duncan Sands9dff9be2010-02-15 16:12:20 +00002379 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002380 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2381 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002382}
2383
2384/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002385CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002386 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002387 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002388 assert(S->getType()->isPointerTy() && "Invalid cast");
2389 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002390 "Invalid cast");
2391
Duncan Sands9dff9be2010-02-15 16:12:20 +00002392 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002393 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2394 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002395}
2396
Chris Lattner229907c2011-07-18 04:54:35 +00002397CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002398 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002399 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002400 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002401 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002402 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2403 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002404 Instruction::CastOps opcode =
2405 (SrcBits == DstBits ? Instruction::BitCast :
2406 (SrcBits > DstBits ? Instruction::Trunc :
2407 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002408 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002409}
2410
Chris Lattner229907c2011-07-18 04:54:35 +00002411CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002412 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002413 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002414 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002415 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002416 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2417 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002418 Instruction::CastOps opcode =
2419 (SrcBits == DstBits ? Instruction::BitCast :
2420 (SrcBits > DstBits ? Instruction::Trunc :
2421 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002422 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002423}
2424
Chris Lattner229907c2011-07-18 04:54:35 +00002425CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002426 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002427 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002428 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002429 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002430 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2431 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002432 Instruction::CastOps opcode =
2433 (SrcBits == DstBits ? Instruction::BitCast :
2434 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002435 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002436}
2437
Chris Lattner229907c2011-07-18 04:54:35 +00002438CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002439 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002440 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002441 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002442 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002443 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2444 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002445 Instruction::CastOps opcode =
2446 (SrcBits == DstBits ? Instruction::BitCast :
2447 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002448 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002449}
2450
Duncan Sands55e50902008-01-06 10:12:28 +00002451// Check whether it is valid to call getCastOpcode for these types.
2452// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002453bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002454 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2455 return false;
2456
2457 if (SrcTy == DestTy)
2458 return true;
2459
Chris Lattner229907c2011-07-18 04:54:35 +00002460 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2461 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002462 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2463 // An element by element cast. Valid if casting the elements is valid.
2464 SrcTy = SrcVecTy->getElementType();
2465 DestTy = DestVecTy->getElementType();
2466 }
2467
Duncan Sands55e50902008-01-06 10:12:28 +00002468 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002469 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2470 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002471
2472 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002473 if (DestTy->isIntegerTy()) { // Casting to integral
2474 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002475 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002476 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002477 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002478 } else if (SrcTy->isVectorTy()) { // Casting from vector
2479 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002480 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002481 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002482 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002483 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
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 Sands55e50902008-01-06 10:12:28 +00002491 return false;
2492 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002493 } else if (DestTy->isVectorTy()) { // Casting to vector
2494 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002495 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002496 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002497 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002498 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002499 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002500 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002501 return false;
2502 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002503 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002504 if (SrcTy->isVectorTy()) {
2505 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002506 } else {
2507 return false;
2508 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002509 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002510 return false;
2511 }
2512}
2513
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514// Provide a way to get a "cast" where the cast opcode is inferred from the
2515// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002516// logic in the castIsValid function below. This axiom should hold:
2517// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2518// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002520// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002522CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002523 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2524 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525
Duncan Sands55e50902008-01-06 10:12:28 +00002526 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2527 "Only first class types are castable!");
2528
Duncan Sandsa8514532011-05-18 07:13:41 +00002529 if (SrcTy == DestTy)
2530 return BitCast;
2531
Chris Lattner229907c2011-07-18 04:54:35 +00002532 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2533 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002534 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2535 // An element by element cast. Find the appropriate opcode based on the
2536 // element types.
2537 SrcTy = SrcVecTy->getElementType();
2538 DestTy = DestVecTy->getElementType();
2539 }
2540
2541 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002542 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2543 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002544
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002546 if (DestTy->isIntegerTy()) { // Casting to integral
2547 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548 if (DestBits < SrcBits)
2549 return Trunc; // int -> smaller int
2550 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002551 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552 return SExt; // signed -> SEXT
2553 else
2554 return ZExt; // unsigned -> ZEXT
2555 } else {
2556 return BitCast; // Same size, No-op cast
2557 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002558 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002559 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560 return FPToSI; // FP -> sint
2561 else
2562 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002563 } else if (SrcTy->isVectorTy()) {
2564 assert(DestBits == SrcBits &&
2565 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566 return BitCast; // Same size, no-op cast
2567 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002568 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569 "Casting from a value that is not first-class type");
2570 return PtrToInt; // ptr -> int
2571 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002572 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2573 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002574 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575 return SIToFP; // sint -> FP
2576 else
2577 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002578 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579 if (DestBits < SrcBits) {
2580 return FPTrunc; // FP -> smaller FP
2581 } else if (DestBits > SrcBits) {
2582 return FPExt; // FP -> larger FP
2583 } else {
2584 return BitCast; // same size, no-op cast
2585 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002586 } else if (SrcTy->isVectorTy()) {
2587 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002588 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002589 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002590 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002591 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002592 } else if (DestTy->isVectorTy()) {
2593 assert(DestBits == SrcBits &&
2594 "Illegal cast to vector (wrong type or size)");
2595 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002596 } else if (DestTy->isPointerTy()) {
2597 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002599 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002602 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002603 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002604 if (SrcTy->isVectorTy()) {
2605 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002606 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002607 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002608 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002610 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611}
2612
2613//===----------------------------------------------------------------------===//
2614// CastInst SubClass Constructors
2615//===----------------------------------------------------------------------===//
2616
2617/// Check that the construction parameters for a CastInst are correct. This
2618/// could be broken out into the separate constructors but it is useful to have
2619/// it in one place and to eliminate the redundant code for getting the sizes
2620/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002621bool
Chris Lattner229907c2011-07-18 04:54:35 +00002622CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623
2624 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002625 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002626 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2627 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628 return false;
2629
2630 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002631 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2632 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633
Duncan Sands7f646562011-05-18 09:21:57 +00002634 // If these are vector types, get the lengths of the vectors (using zero for
2635 // scalar types means that checking that vector lengths match also checks that
2636 // scalars are not being converted to vectors or vectors to scalars).
2637 unsigned SrcLength = SrcTy->isVectorTy() ?
2638 cast<VectorType>(SrcTy)->getNumElements() : 0;
2639 unsigned DstLength = DstTy->isVectorTy() ?
2640 cast<VectorType>(DstTy)->getNumElements() : 0;
2641
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642 // Switch on the opcode provided
2643 switch (op) {
2644 default: return false; // This is an input error
2645 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002646 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2647 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002649 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2650 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002651 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002652 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2653 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002655 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2656 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002658 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2659 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002662 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2663 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002664 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002666 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2667 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002668 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002669 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002670 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002671 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2672 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2673 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002674 return SrcTy->getScalarType()->isPointerTy() &&
2675 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002676 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002677 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002678 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002679 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2680 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2681 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002682 return SrcTy->getScalarType()->isIntegerTy() &&
2683 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002684 case Instruction::BitCast:
2685 // BitCast implies a no-op cast of type only. No bits change.
2686 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002687 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002688 return false;
2689
Duncan Sands55e50902008-01-06 10:12:28 +00002690 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002691 // these cases, the cast is okay if the source and destination bit widths
2692 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002693 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002694 }
2695}
2696
2697TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002698 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002699) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002700 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002701}
2702
2703TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002704 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002705) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002706 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002707}
2708
2709ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002710 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002711) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002712 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002713}
2714
2715ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002716 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002717) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002718 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002719}
2720SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002721 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002722) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002723 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002724}
2725
Jeff Cohencc08c832006-12-02 02:22:01 +00002726SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002727 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002729 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002730}
2731
2732FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002733 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002734) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002735 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002736}
2737
2738FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002739 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002740) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002741 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002742}
2743
2744FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002745 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002746) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002747 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002748}
2749
2750FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002751 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002752) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002753 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002754}
2755
2756UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002757 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002758) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002759 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002760}
2761
2762UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002763 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002764) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002765 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002766}
2767
2768SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002769 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002770) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002771 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002772}
2773
2774SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002775 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002776) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002777 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002778}
2779
2780FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002781 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002782) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002783 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002784}
2785
2786FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002787 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002788) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002789 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002790}
2791
2792FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002793 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002794) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002795 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002796}
2797
2798FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002799 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002800) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002801 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002802}
2803
2804PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002805 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002806) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002807 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002808}
2809
2810PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002811 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002812) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002813 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002814}
2815
2816IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002817 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002818) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002819 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002820}
2821
2822IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002823 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002824) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002825 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002826}
2827
2828BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002829 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002830) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002831 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002832}
2833
2834BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002835 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002836) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002837 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002838}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002839
2840//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002841// CmpInst Classes
2842//===----------------------------------------------------------------------===//
2843
Craig Topper3186c012012-09-23 02:12:10 +00002844void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002845
Chris Lattner229907c2011-07-18 04:54:35 +00002846CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002847 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002848 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002849 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002850 OperandTraits<CmpInst>::op_begin(this),
2851 OperandTraits<CmpInst>::operands(this),
2852 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002853 Op<0>() = LHS;
2854 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002855 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002856 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002857}
Gabor Greiff6caff662008-05-10 08:32:32 +00002858
Chris Lattner229907c2011-07-18 04:54:35 +00002859CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002860 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002861 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002862 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002863 OperandTraits<CmpInst>::op_begin(this),
2864 OperandTraits<CmpInst>::operands(this),
2865 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002866 Op<0>() = LHS;
2867 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002868 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002869 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002870}
2871
2872CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002873CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002874 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002875 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002876 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002877 if (InsertBefore)
2878 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2879 S1, S2, Name);
2880 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002881 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002882 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002883 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002884
2885 if (InsertBefore)
2886 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2887 S1, S2, Name);
2888 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002889 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002890 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002891}
2892
2893CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002894CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002895 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002896 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002897 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2898 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002899 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002900 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2901 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002902}
2903
2904void CmpInst::swapOperands() {
2905 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2906 IC->swapOperands();
2907 else
2908 cast<FCmpInst>(this)->swapOperands();
2909}
2910
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002911bool CmpInst::isCommutative() const {
2912 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002913 return IC->isCommutative();
2914 return cast<FCmpInst>(this)->isCommutative();
2915}
2916
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002917bool CmpInst::isEquality() const {
2918 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002919 return IC->isEquality();
2920 return cast<FCmpInst>(this)->isEquality();
2921}
2922
2923
Dan Gohman4e724382008-05-31 02:47:54 +00002924CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002925 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002926 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002927 case ICMP_EQ: return ICMP_NE;
2928 case ICMP_NE: return ICMP_EQ;
2929 case ICMP_UGT: return ICMP_ULE;
2930 case ICMP_ULT: return ICMP_UGE;
2931 case ICMP_UGE: return ICMP_ULT;
2932 case ICMP_ULE: return ICMP_UGT;
2933 case ICMP_SGT: return ICMP_SLE;
2934 case ICMP_SLT: return ICMP_SGE;
2935 case ICMP_SGE: return ICMP_SLT;
2936 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002937
Dan Gohman4e724382008-05-31 02:47:54 +00002938 case FCMP_OEQ: return FCMP_UNE;
2939 case FCMP_ONE: return FCMP_UEQ;
2940 case FCMP_OGT: return FCMP_ULE;
2941 case FCMP_OLT: return FCMP_UGE;
2942 case FCMP_OGE: return FCMP_ULT;
2943 case FCMP_OLE: return FCMP_UGT;
2944 case FCMP_UEQ: return FCMP_ONE;
2945 case FCMP_UNE: return FCMP_OEQ;
2946 case FCMP_UGT: return FCMP_OLE;
2947 case FCMP_ULT: return FCMP_OGE;
2948 case FCMP_UGE: return FCMP_OLT;
2949 case FCMP_ULE: return FCMP_OGT;
2950 case FCMP_ORD: return FCMP_UNO;
2951 case FCMP_UNO: return FCMP_ORD;
2952 case FCMP_TRUE: return FCMP_FALSE;
2953 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002954 }
2955}
2956
Reid Spencer266e42b2006-12-23 06:05:41 +00002957ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2958 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002959 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002960 case ICMP_EQ: case ICMP_NE:
2961 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2962 return pred;
2963 case ICMP_UGT: return ICMP_SGT;
2964 case ICMP_ULT: return ICMP_SLT;
2965 case ICMP_UGE: return ICMP_SGE;
2966 case ICMP_ULE: return ICMP_SLE;
2967 }
2968}
2969
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002970ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2971 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002972 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002973 case ICMP_EQ: case ICMP_NE:
2974 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2975 return pred;
2976 case ICMP_SGT: return ICMP_UGT;
2977 case ICMP_SLT: return ICMP_ULT;
2978 case ICMP_SGE: return ICMP_UGE;
2979 case ICMP_SLE: return ICMP_ULE;
2980 }
2981}
2982
Reid Spencer0286bc12007-02-28 22:00:54 +00002983/// Initialize a set of values that all satisfy the condition with C.
2984///
2985ConstantRange
2986ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2987 APInt Lower(C);
2988 APInt Upper(C);
2989 uint32_t BitWidth = C.getBitWidth();
2990 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002991 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002992 case ICmpInst::ICMP_EQ: Upper++; break;
2993 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002994 case ICmpInst::ICMP_ULT:
2995 Lower = APInt::getMinValue(BitWidth);
2996 // Check for an empty-set condition.
2997 if (Lower == Upper)
2998 return ConstantRange(BitWidth, /*isFullSet=*/false);
2999 break;
3000 case ICmpInst::ICMP_SLT:
3001 Lower = APInt::getSignedMinValue(BitWidth);
3002 // Check for an empty-set condition.
3003 if (Lower == Upper)
3004 return ConstantRange(BitWidth, /*isFullSet=*/false);
3005 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003006 case ICmpInst::ICMP_UGT:
3007 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003008 // Check for an empty-set condition.
3009 if (Lower == Upper)
3010 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003011 break;
3012 case ICmpInst::ICMP_SGT:
3013 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003014 // Check for an empty-set condition.
3015 if (Lower == Upper)
3016 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003017 break;
3018 case ICmpInst::ICMP_ULE:
3019 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003020 // Check for a full-set condition.
3021 if (Lower == Upper)
3022 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003023 break;
3024 case ICmpInst::ICMP_SLE:
3025 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003026 // Check for a full-set condition.
3027 if (Lower == Upper)
3028 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003029 break;
3030 case ICmpInst::ICMP_UGE:
3031 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003032 // Check for a full-set condition.
3033 if (Lower == Upper)
3034 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003035 break;
3036 case ICmpInst::ICMP_SGE:
3037 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003038 // Check for a full-set condition.
3039 if (Lower == Upper)
3040 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003041 break;
3042 }
3043 return ConstantRange(Lower, Upper);
3044}
3045
Dan Gohman4e724382008-05-31 02:47:54 +00003046CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003047 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003048 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003049 case ICMP_EQ: case ICMP_NE:
3050 return pred;
3051 case ICMP_SGT: return ICMP_SLT;
3052 case ICMP_SLT: return ICMP_SGT;
3053 case ICMP_SGE: return ICMP_SLE;
3054 case ICMP_SLE: return ICMP_SGE;
3055 case ICMP_UGT: return ICMP_ULT;
3056 case ICMP_ULT: return ICMP_UGT;
3057 case ICMP_UGE: return ICMP_ULE;
3058 case ICMP_ULE: return ICMP_UGE;
3059
Reid Spencerd9436b62006-11-20 01:22:35 +00003060 case FCMP_FALSE: case FCMP_TRUE:
3061 case FCMP_OEQ: case FCMP_ONE:
3062 case FCMP_UEQ: case FCMP_UNE:
3063 case FCMP_ORD: case FCMP_UNO:
3064 return pred;
3065 case FCMP_OGT: return FCMP_OLT;
3066 case FCMP_OLT: return FCMP_OGT;
3067 case FCMP_OGE: return FCMP_OLE;
3068 case FCMP_OLE: return FCMP_OGE;
3069 case FCMP_UGT: return FCMP_ULT;
3070 case FCMP_ULT: return FCMP_UGT;
3071 case FCMP_UGE: return FCMP_ULE;
3072 case FCMP_ULE: return FCMP_UGE;
3073 }
3074}
3075
Reid Spencer266e42b2006-12-23 06:05:41 +00003076bool CmpInst::isUnsigned(unsigned short predicate) {
3077 switch (predicate) {
3078 default: return false;
3079 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3080 case ICmpInst::ICMP_UGE: return true;
3081 }
3082}
3083
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003084bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003085 switch (predicate) {
3086 default: return false;
3087 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3088 case ICmpInst::ICMP_SGE: return true;
3089 }
3090}
3091
3092bool CmpInst::isOrdered(unsigned short predicate) {
3093 switch (predicate) {
3094 default: return false;
3095 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3096 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3097 case FCmpInst::FCMP_ORD: return true;
3098 }
3099}
3100
3101bool CmpInst::isUnordered(unsigned short predicate) {
3102 switch (predicate) {
3103 default: return false;
3104 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3105 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3106 case FCmpInst::FCMP_UNO: return true;
3107 }
3108}
3109
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003110bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3111 switch(predicate) {
3112 default: return false;
3113 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3114 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3115 }
3116}
3117
3118bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3119 switch(predicate) {
3120 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3121 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3122 default: return false;
3123 }
3124}
3125
3126
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003127//===----------------------------------------------------------------------===//
3128// SwitchInst Implementation
3129//===----------------------------------------------------------------------===//
3130
Chris Lattnerbaf00152010-11-17 05:41:46 +00003131void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3132 assert(Value && Default && NumReserved);
3133 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003134 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003135 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003136
Gabor Greif2d3024d2008-05-26 21:33:52 +00003137 OperandList[0] = Value;
3138 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003139}
3140
Chris Lattner2195fc42007-02-24 00:55:48 +00003141/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3142/// switch on and a default destination. The number of additional cases can
3143/// be specified here to make memory allocation more efficient. This
3144/// constructor can also autoinsert before another instruction.
3145SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3146 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003147 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3148 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003149 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003150}
3151
3152/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3153/// switch on and a default destination. The number of additional cases can
3154/// be specified here to make memory allocation more efficient. This
3155/// constructor also autoinserts at the end of the specified BasicBlock.
3156SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3157 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003158 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3159 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003160 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003161}
3162
Misha Brukmanb1c93172005-04-21 23:48:37 +00003163SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003164 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3165 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3166 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003167 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003168 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003169 OL[i] = InOL[i];
3170 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003171 }
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003172 TheSubsets = SI.TheSubsets;
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003173 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003174}
3175
Gordon Henriksen14a55692007-12-10 02:14:30 +00003176SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003177 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003178}
3179
3180
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003181/// addCase - Add an entry to the switch instruction...
3182///
Chris Lattner47ac1872005-02-24 05:32:09 +00003183void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003184 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00003185
3186 // FIXME: Currently we work with ConstantInt based cases.
3187 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003188 Mapping.add(IntItem::fromConstantInt(OnVal));
3189 IntegersSubset CaseRanges = Mapping.getCase();
3190 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00003191}
3192
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003193void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003194 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003195 unsigned OpNo = NumOperands;
3196 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003197 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003198 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003199 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003200 NumOperands = OpNo+2;
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003201
3202 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3203
3204 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3205 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003206 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003207}
3208
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003209/// removeCase - This method removes the specified case and its successor
3210/// from the switch instruction.
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003211void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003212 unsigned idx = i.getCaseIndex();
3213
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003214 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003215
3216 unsigned NumOps = getNumOperands();
3217 Use *OL = OperandList;
3218
Jay Foad14277722011-02-01 09:22:34 +00003219 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003220 if (2 + (idx + 1) * 2 != NumOps) {
3221 OL[2 + idx * 2] = OL[NumOps - 2];
3222 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003223 }
3224
3225 // Nuke the last value.
3226 OL[NumOps-2].set(0);
3227 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003228
3229 // Do the same with TheCases collection:
3230 if (i.SubsetIt != --TheSubsets.end()) {
3231 *i.SubsetIt = TheSubsets.back();
3232 TheSubsets.pop_back();
3233 } else {
3234 TheSubsets.pop_back();
3235 i.SubsetIt = TheSubsets.end();
3236 }
3237
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003238 NumOperands = NumOps-2;
3239}
3240
Jay Foade98f29d2011-04-01 08:00:58 +00003241/// growOperands - grow operands - This grows the operand list in response
3242/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003243///
Jay Foade98f29d2011-04-01 08:00:58 +00003244void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003245 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003246 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003247
3248 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003249 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003250 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003251 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003252 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003253 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003254 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003255 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003256}
3257
3258
3259BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3260 return getSuccessor(idx);
3261}
3262unsigned SwitchInst::getNumSuccessorsV() const {
3263 return getNumSuccessors();
3264}
3265void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3266 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003267}
Chris Lattnerf22be932004-10-15 23:52:53 +00003268
Chris Lattner3ed871f2009-10-27 19:13:16 +00003269//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003270// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003271//===----------------------------------------------------------------------===//
3272
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003273void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003274 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003275 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003276 ReservedSpace = 1+NumDests;
3277 NumOperands = 1;
3278 OperandList = allocHungoffUses(ReservedSpace);
3279
3280 OperandList[0] = Address;
3281}
3282
3283
Jay Foade98f29d2011-04-01 08:00:58 +00003284/// growOperands - grow operands - This grows the operand list in response
3285/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003286///
Jay Foade98f29d2011-04-01 08:00:58 +00003287void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003288 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003289 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003290
3291 ReservedSpace = NumOps;
3292 Use *NewOps = allocHungoffUses(NumOps);
3293 Use *OldOps = OperandList;
3294 for (unsigned i = 0; i != e; ++i)
3295 NewOps[i] = OldOps[i];
3296 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003297 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003298}
3299
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003300IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3301 Instruction *InsertBefore)
3302: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003303 0, 0, InsertBefore) {
3304 init(Address, NumCases);
3305}
3306
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003307IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3308 BasicBlock *InsertAtEnd)
3309: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003310 0, 0, InsertAtEnd) {
3311 init(Address, NumCases);
3312}
3313
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003314IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3315 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003316 allocHungoffUses(IBI.getNumOperands()),
3317 IBI.getNumOperands()) {
3318 Use *OL = OperandList, *InOL = IBI.OperandList;
3319 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3320 OL[i] = InOL[i];
3321 SubclassOptionalData = IBI.SubclassOptionalData;
3322}
3323
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003324IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003325 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003326}
3327
3328/// addDestination - Add a destination.
3329///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003330void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003331 unsigned OpNo = NumOperands;
3332 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003333 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003334 // Initialize some new operands.
3335 assert(OpNo < ReservedSpace && "Growing didn't work!");
3336 NumOperands = OpNo+1;
3337 OperandList[OpNo] = DestBB;
3338}
3339
3340/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003341/// indirectbr instruction.
3342void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003343 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3344
3345 unsigned NumOps = getNumOperands();
3346 Use *OL = OperandList;
3347
3348 // Replace this value with the last one.
3349 OL[idx+1] = OL[NumOps-1];
3350
3351 // Nuke the last value.
3352 OL[NumOps-1].set(0);
3353 NumOperands = NumOps-1;
3354}
3355
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003356BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003357 return getSuccessor(idx);
3358}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003359unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003360 return getNumSuccessors();
3361}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003362void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003363 setSuccessor(idx, B);
3364}
3365
3366//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003367// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003368//===----------------------------------------------------------------------===//
3369
Chris Lattnerf22be932004-10-15 23:52:53 +00003370// Define these methods here so vtables don't get emitted into every translation
3371// unit that uses these classes.
3372
Devang Patel11cf3f42009-10-27 22:16:29 +00003373GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3374 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003375}
3376
Devang Patel11cf3f42009-10-27 22:16:29 +00003377BinaryOperator *BinaryOperator::clone_impl() const {
3378 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003379}
3380
Devang Patel11cf3f42009-10-27 22:16:29 +00003381FCmpInst* FCmpInst::clone_impl() const {
3382 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003383}
3384
Devang Patel11cf3f42009-10-27 22:16:29 +00003385ICmpInst* ICmpInst::clone_impl() const {
3386 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003387}
3388
Devang Patel11cf3f42009-10-27 22:16:29 +00003389ExtractValueInst *ExtractValueInst::clone_impl() const {
3390 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003391}
3392
Devang Patel11cf3f42009-10-27 22:16:29 +00003393InsertValueInst *InsertValueInst::clone_impl() const {
3394 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003395}
3396
Devang Patel11cf3f42009-10-27 22:16:29 +00003397AllocaInst *AllocaInst::clone_impl() const {
3398 return new AllocaInst(getAllocatedType(),
3399 (Value*)getOperand(0),
3400 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003401}
3402
Devang Patel11cf3f42009-10-27 22:16:29 +00003403LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003404 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3405 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003406}
3407
Devang Patel11cf3f42009-10-27 22:16:29 +00003408StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003409 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003410 getAlignment(), getOrdering(), getSynchScope());
3411
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003412}
3413
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003414AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3415 AtomicCmpXchgInst *Result =
3416 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3417 getOrdering(), getSynchScope());
3418 Result->setVolatile(isVolatile());
3419 return Result;
3420}
3421
3422AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3423 AtomicRMWInst *Result =
3424 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3425 getOrdering(), getSynchScope());
3426 Result->setVolatile(isVolatile());
3427 return Result;
3428}
3429
Eli Friedmanfee02c62011-07-25 23:16:38 +00003430FenceInst *FenceInst::clone_impl() const {
3431 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3432}
3433
Devang Patel11cf3f42009-10-27 22:16:29 +00003434TruncInst *TruncInst::clone_impl() const {
3435 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003436}
3437
Devang Patel11cf3f42009-10-27 22:16:29 +00003438ZExtInst *ZExtInst::clone_impl() const {
3439 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003440}
3441
Devang Patel11cf3f42009-10-27 22:16:29 +00003442SExtInst *SExtInst::clone_impl() const {
3443 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003444}
3445
Devang Patel11cf3f42009-10-27 22:16:29 +00003446FPTruncInst *FPTruncInst::clone_impl() const {
3447 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003448}
3449
Devang Patel11cf3f42009-10-27 22:16:29 +00003450FPExtInst *FPExtInst::clone_impl() const {
3451 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003452}
3453
Devang Patel11cf3f42009-10-27 22:16:29 +00003454UIToFPInst *UIToFPInst::clone_impl() const {
3455 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003456}
3457
Devang Patel11cf3f42009-10-27 22:16:29 +00003458SIToFPInst *SIToFPInst::clone_impl() const {
3459 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003460}
3461
Devang Patel11cf3f42009-10-27 22:16:29 +00003462FPToUIInst *FPToUIInst::clone_impl() const {
3463 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003464}
3465
Devang Patel11cf3f42009-10-27 22:16:29 +00003466FPToSIInst *FPToSIInst::clone_impl() const {
3467 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003468}
3469
Devang Patel11cf3f42009-10-27 22:16:29 +00003470PtrToIntInst *PtrToIntInst::clone_impl() const {
3471 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003472}
3473
Devang Patel11cf3f42009-10-27 22:16:29 +00003474IntToPtrInst *IntToPtrInst::clone_impl() const {
3475 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003476}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003477
Devang Patel11cf3f42009-10-27 22:16:29 +00003478BitCastInst *BitCastInst::clone_impl() const {
3479 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003480}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003481
Devang Patel11cf3f42009-10-27 22:16:29 +00003482CallInst *CallInst::clone_impl() const {
3483 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003484}
3485
Devang Patel11cf3f42009-10-27 22:16:29 +00003486SelectInst *SelectInst::clone_impl() const {
3487 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003488}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003489
Devang Patel11cf3f42009-10-27 22:16:29 +00003490VAArgInst *VAArgInst::clone_impl() const {
3491 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003492}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003493
Devang Patel11cf3f42009-10-27 22:16:29 +00003494ExtractElementInst *ExtractElementInst::clone_impl() const {
3495 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003496}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003497
Devang Patel11cf3f42009-10-27 22:16:29 +00003498InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003499 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003500}
3501
Devang Patel11cf3f42009-10-27 22:16:29 +00003502ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003503 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003504}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003505
Devang Patel11cf3f42009-10-27 22:16:29 +00003506PHINode *PHINode::clone_impl() const {
3507 return new PHINode(*this);
3508}
3509
Bill Wendlingfae14752011-08-12 20:24:12 +00003510LandingPadInst *LandingPadInst::clone_impl() const {
3511 return new LandingPadInst(*this);
3512}
3513
Devang Patel11cf3f42009-10-27 22:16:29 +00003514ReturnInst *ReturnInst::clone_impl() const {
3515 return new(getNumOperands()) ReturnInst(*this);
3516}
3517
3518BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003519 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003520}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003521
Devang Patel11cf3f42009-10-27 22:16:29 +00003522SwitchInst *SwitchInst::clone_impl() const {
3523 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003524}
3525
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003526IndirectBrInst *IndirectBrInst::clone_impl() const {
3527 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003528}
3529
3530
Devang Patel11cf3f42009-10-27 22:16:29 +00003531InvokeInst *InvokeInst::clone_impl() const {
3532 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003533}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003534
Bill Wendlingf891bf82011-07-31 06:30:59 +00003535ResumeInst *ResumeInst::clone_impl() const {
3536 return new(1) ResumeInst(*this);
3537}
3538
Devang Patel11cf3f42009-10-27 22:16:29 +00003539UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003540 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003541 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003542}