blob: f2e9813bc638848e0245a2731239ff3ed4717eee [file] [log] [blame]
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerb12261a2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos91366a82004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Patel6c6c0162009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
18#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/Operator.h"
Alkis Evlogimenos91366a82004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer3da43842007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb43c7f372007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos91366a82004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner50ee9dd2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifc9f75002010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif3ecf3552010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greifa6aac4c2010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifc9f75002010-03-24 13:21:49 +000038}
39
Gordon Henriksenafba8fe2007-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 Greifefe65362008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerb12261a2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattnerb76ec322008-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 Lattnerdb125cf2011-07-18 04:54:35 +000066 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattnerb76ec322008-12-29 00:12:50 +000067 // Vector select.
Owen Anderson1d0be152009-08-13 21:58:54 +000068 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattnerb76ec322008-12-29 00:12:50 +000069 return "vector select condition element type must be i1";
Chris Lattnerdb125cf2011-07-18 04:54:35 +000070 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Chris Lattnerb76ec322008-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 Anderson1d0be152009-08-13 21:58:54 +000076 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattnerb76ec322008-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 Lattnerb12261a2005-01-29 00:35:16 +000084// PHINode Class
85//===----------------------------------------------------------------------===//
86
87PHINode::PHINode(const PHINode &PN)
88 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greifefe65362008-05-10 08:32:32 +000089 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerb12261a2005-01-29 00:35:16 +000090 ReservedSpace(PN.getNumOperands()) {
Jay Foad95c3e482011-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 Gohman58cfa3b2009-08-25 22:11:20 +000093 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerb12261a2005-01-29 00:35:16 +000094}
95
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000096PHINode::~PHINode() {
Jay Foad1ed26ac2011-01-16 15:30:52 +000097 dropHungoffUses();
Chris Lattnerb12261a2005-01-29 00:35:16 +000098}
99
Jay Foad95c3e482011-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 Lattnerb12261a2005-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 Foad95c3e482011-06-23 09:09:15 +0000115 Value *Removed = getIncomingValue(Idx);
Chris Lattnerb12261a2005-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 Foad95c3e482011-06-23 09:09:15 +0000120 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerb12261a2005-01-29 00:35:16 +0000121 // use/def lists, which is kinda lame.
Jay Foad95c3e482011-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 Lattnerb12261a2005-01-29 00:35:16 +0000124
125 // Nuke the last value.
Jay Foad95c3e482011-06-23 09:09:15 +0000126 Op<-1>().set(0);
127 --NumOperands;
Chris Lattnerb12261a2005-01-29 00:35:16 +0000128
129 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad95c3e482011-06-23 09:09:15 +0000130 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerb12261a2005-01-29 00:35:16 +0000131 // If anyone is using this PHI, make them use a dummy value instead...
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000132 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerb12261a2005-01-29 00:35:16 +0000133 eraseFromParent();
134 }
135 return Removed;
136}
137
Jay Foad8891ed72011-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 Lattnerb12261a2005-01-29 00:35:16 +0000141///
Jay Foad8891ed72011-04-01 08:00:58 +0000142void PHINode::growOperands() {
Gabor Greifefe65362008-05-10 08:32:32 +0000143 unsigned e = getNumOperands();
Jay Foad95c3e482011-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 Foad72f5f312011-06-20 14:38:01 +0000149
Chris Lattnerb12261a2005-01-29 00:35:16 +0000150 ReservedSpace = NumOps;
Jay Foad95c3e482011-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 Foad1ed26ac2011-01-16 15:30:52 +0000156 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerb12261a2005-01-29 00:35:16 +0000157}
158
Nate Begemana83ba0f2005-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 Sandsff103412010-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 Lopes44d5c062012-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 Lopes0fd518b2012-07-03 21:15:40 +0000171 if (ConstantValue == this)
172 return UndefValue::get(getType());
Duncan Sandsff103412010-11-17 04:30:22 +0000173 return ConstantValue;
Nate Begemana83ba0f2005-08-04 23:24:19 +0000174}
175
Bill Wendlinge6e88262011-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 Lattnerb12261a2005-01-29 00:35:16 +0000258
259//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000260// CallInst Implementation
261//===----------------------------------------------------------------------===//
262
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000263CallInst::~CallInst() {
Chris Lattnerb12261a2005-01-29 00:35:16 +0000264}
265
Jay Foada3efbb12011-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 Greifa6aac4c2010-07-16 09:38:02 +0000268 Op<-1>() = Func;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000269
Jay Foada3efbb12011-07-15 08:37:34 +0000270#ifndef NDEBUG
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000271 FunctionType *FTy =
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000272 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
273
Jay Foada3efbb12011-07-15 08:37:34 +0000274 assert((Args.size() == FTy->getNumParams() ||
275 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner9b4c96d2006-05-03 00:48:22 +0000276 "Calling a function with bad signature!");
Jay Foada3efbb12011-07-15 08:37:34 +0000277
278 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner9b4c96d2006-05-03 00:48:22 +0000279 assert((i >= FTy->getNumParams() ||
Jay Foada3efbb12011-07-15 08:37:34 +0000280 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner9b4c96d2006-05-03 00:48:22 +0000281 "Calling a function with a bad signature!");
Jay Foada3efbb12011-07-15 08:37:34 +0000282#endif
283
284 std::copy(Args.begin(), Args.end(), op_begin());
285 setName(NameStr);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000286}
287
Jay Foada3efbb12011-07-15 08:37:34 +0000288void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greifefe65362008-05-10 08:32:32 +0000289 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000290 Op<-1>() = Func;
Misha Brukmanfd939082005-04-21 23:48:37 +0000291
Jay Foada3efbb12011-07-15 08:37:34 +0000292#ifndef NDEBUG
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000293 FunctionType *FTy =
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000294 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
295
Chris Lattner2e21fce2007-02-01 04:59:37 +0000296 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foada3efbb12011-07-15 08:37:34 +0000297#endif
298
299 setName(NameStr);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000300}
301
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000302CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000303 Instruction *InsertBefore)
304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
Gabor Greifefe65362008-05-10 08:32:32 +0000306 Instruction::Call,
307 OperandTraits<CallInst>::op_end(this) - 1,
308 1, InsertBefore) {
Jay Foada3efbb12011-07-15 08:37:34 +0000309 init(Func, Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000310}
311
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000312CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000313 BasicBlock *InsertAtEnd)
314 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
315 ->getElementType())->getReturnType(),
Gabor Greifefe65362008-05-10 08:32:32 +0000316 Instruction::Call,
317 OperandTraits<CallInst>::op_end(this) - 1,
318 1, InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +0000319 init(Func, Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000320}
321
Misha Brukmanfd939082005-04-21 23:48:37 +0000322CallInst::CallInst(const CallInst &CI)
Gabor Greifefe65362008-05-10 08:32:32 +0000323 : Instruction(CI.getType(), Instruction::Call,
324 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner58d74912008-03-12 17:45:29 +0000325 CI.getNumOperands()) {
Devang Patel05988662008-09-25 21:00:45 +0000326 setAttributes(CI.getAttributes());
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000327 setTailCall(CI.isTailCall());
328 setCallingConv(CI.getCallingConv());
329
Jay Foada3efbb12011-07-15 08:37:34 +0000330 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000331 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000332}
333
Bill Wendling034b94b2012-12-19 07:18:57 +0000334void CallInst::addAttribute(unsigned i, Attribute attr) {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000335 AttributeSet PAL = getAttributes();
Bill Wendlingc4167952012-10-14 07:35:59 +0000336 PAL = PAL.addAttr(getContext(), i, attr);
Devang Patel05988662008-09-25 21:00:45 +0000337 setAttributes(PAL);
Eric Christopher0bf7b412008-05-16 20:39:43 +0000338}
339
Bill Wendling034b94b2012-12-19 07:18:57 +0000340void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000341 AttributeSet PAL = getAttributes();
Bill Wendling5886b7b2012-10-14 06:39:53 +0000342 PAL = PAL.removeAttr(getContext(), i, attr);
Devang Patel05988662008-09-25 21:00:45 +0000343 setAttributes(PAL);
Duncan Sands2e033f32008-07-08 08:38:44 +0000344}
345
Bill Wendling629fb822012-12-22 00:37:52 +0000346bool CallInst::hasFnAttr(Attribute::AttrKind A) const {
Bill Wendling94e94b32012-12-30 13:50:49 +0000347 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling060f20a2012-10-09 00:28:54 +0000348 return true;
349 if (const Function *F = getCalledFunction())
Bill Wendling39cd0c82012-12-30 12:45:13 +0000350 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling060f20a2012-10-09 00:28:54 +0000351 return false;
352}
353
Bill Wendling629fb822012-12-22 00:37:52 +0000354bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling94e94b32012-12-30 13:50:49 +0000355 if (AttributeList.hasAttribute(i, A))
Bill Wendling847d1652012-10-03 17:54:26 +0000356 return true;
357 if (const Function *F = getCalledFunction())
Bill Wendling39cd0c82012-12-30 12:45:13 +0000358 return F->getAttributes().hasAttribute(i, A);
Bill Wendling365b9e02012-10-04 07:18:12 +0000359 return false;
360}
361
Evan Chengfabcb912009-09-10 04:36:43 +0000362/// IsConstantOne - Return true only if val is constant int 1
363static bool IsConstantOne(Value *val) {
364 assert(val && "IsConstantOne does not work with NULL val");
365 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
366}
367
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000368static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000369 BasicBlock *InsertAtEnd, Type *IntPtrTy,
370 Type *AllocTy, Value *AllocSize,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000371 Value *ArraySize, Function *MallocF,
372 const Twine &Name) {
Benjamin Kramerb84c5ae2009-09-10 11:31:39 +0000373 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez88d98392009-09-18 19:20:02 +0000374 "createMalloc needs either InsertBefore or InsertAtEnd");
375
376 // malloc(type) becomes:
377 // bitcast (i8* malloc(typeSize)) to type*
378 // malloc(type, arraySize) becomes:
379 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000380 if (!ArraySize)
381 ArraySize = ConstantInt::get(IntPtrTy, 1);
382 else if (ArraySize->getType() != IntPtrTy) {
383 if (InsertBefore)
Victor Hernandez4d81d972009-11-07 00:36:50 +0000384 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
385 "", InsertBefore);
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000386 else
Victor Hernandez4d81d972009-11-07 00:36:50 +0000387 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
388 "", InsertAtEnd);
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000389 }
Evan Chengfabcb912009-09-10 04:36:43 +0000390
Benjamin Kramerb84c5ae2009-09-10 11:31:39 +0000391 if (!IsConstantOne(ArraySize)) {
Evan Chengfabcb912009-09-10 04:36:43 +0000392 if (IsConstantOne(AllocSize)) {
393 AllocSize = ArraySize; // Operand * 1 = Operand
394 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
395 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
396 false /*ZExt*/);
397 // Malloc arg is constant product of type size and array size
398 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
399 } else {
Evan Chengfabcb912009-09-10 04:36:43 +0000400 // Multiply type size by the array size...
401 if (InsertBefore)
Victor Hernandez88d98392009-09-18 19:20:02 +0000402 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
403 "mallocsize", InsertBefore);
Evan Chengfabcb912009-09-10 04:36:43 +0000404 else
Victor Hernandez88d98392009-09-18 19:20:02 +0000405 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
406 "mallocsize", InsertAtEnd);
Evan Chengfabcb912009-09-10 04:36:43 +0000407 }
Benjamin Kramerb84c5ae2009-09-10 11:31:39 +0000408 }
Evan Chengfabcb912009-09-10 04:36:43 +0000409
Victor Hernandez88d98392009-09-18 19:20:02 +0000410 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Chengfabcb912009-09-10 04:36:43 +0000411 // Create the call to Malloc.
412 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
413 Module* M = BB->getParent()->getParent();
Chris Lattner1afcace2011-07-09 17:41:24 +0000414 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezf06dca22009-11-10 19:53:28 +0000415 Value *MallocFunc = MallocF;
416 if (!MallocFunc)
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000417 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezf06dca22009-11-10 19:53:28 +0000418 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000419 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Chengfabcb912009-09-10 04:36:43 +0000420 CallInst *MCall = NULL;
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000421 Instruction *Result = NULL;
Victor Hernandez88d98392009-09-18 19:20:02 +0000422 if (InsertBefore) {
Victor Hernandezf06dca22009-11-10 19:53:28 +0000423 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000424 Result = MCall;
425 if (Result->getType() != AllocPtrType)
426 // Create a cast instruction to convert to the right type...
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000427 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez88d98392009-09-18 19:20:02 +0000428 } else {
Victor Hernandezf06dca22009-11-10 19:53:28 +0000429 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000430 Result = MCall;
431 if (Result->getType() != AllocPtrType) {
432 InsertAtEnd->getInstList().push_back(MCall);
433 // Create a cast instruction to convert to the right type...
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000434 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000435 }
Victor Hernandez88d98392009-09-18 19:20:02 +0000436 }
Evan Chengfabcb912009-09-10 04:36:43 +0000437 MCall->setTailCall();
Victor Hernandezf06dca22009-11-10 19:53:28 +0000438 if (Function *F = dyn_cast<Function>(MallocFunc)) {
439 MCall->setCallingConv(F->getCallingConv());
440 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
441 }
Benjamin Kramerf0127052010-01-05 13:12:22 +0000442 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez88d98392009-09-18 19:20:02 +0000443
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000444 return Result;
Evan Chengfabcb912009-09-10 04:36:43 +0000445}
446
447/// CreateMalloc - Generate the IR for a call to malloc:
448/// 1. Compute the malloc call's argument as the specified type's size,
449/// possibly multiplied by the array size if the array size is not
450/// constant 1.
451/// 2. Call malloc with that argument.
452/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000453Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000454 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000455 Value *AllocSize, Value *ArraySize,
Duncan Sands34727662010-07-12 08:16:59 +0000456 Function * MallocF,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000457 const Twine &Name) {
458 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner5a30a852010-07-12 00:57:28 +0000459 ArraySize, MallocF, Name);
Evan Chengfabcb912009-09-10 04:36:43 +0000460}
461
462/// CreateMalloc - Generate the IR for a call to malloc:
463/// 1. Compute the malloc call's argument as the specified type's size,
464/// possibly multiplied by the array size if the array size is not
465/// constant 1.
466/// 2. Call malloc with that argument.
467/// 3. Bitcast the result of the malloc call to the specified type.
468/// Note: This function does not add the bitcast to the basic block, that is the
469/// responsibility of the caller.
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000470Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000471 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000472 Value *AllocSize, Value *ArraySize,
473 Function *MallocF, const Twine &Name) {
474 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000475 ArraySize, MallocF, Name);
Evan Chengfabcb912009-09-10 04:36:43 +0000476}
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000477
Victor Hernandez66284e02009-10-24 04:23:03 +0000478static Instruction* createFree(Value* Source, Instruction *InsertBefore,
479 BasicBlock *InsertAtEnd) {
480 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
481 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands1df98592010-02-16 11:11:14 +0000482 assert(Source->getType()->isPointerTy() &&
Victor Hernandez66284e02009-10-24 04:23:03 +0000483 "Can not free something of nonpointer type!");
484
485 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
486 Module* M = BB->getParent()->getParent();
487
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000488 Type *VoidTy = Type::getVoidTy(M->getContext());
489 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandez66284e02009-10-24 04:23:03 +0000490 // prototype free as "void free(void*)"
Chris Lattner47fc9d32009-11-09 07:12:01 +0000491 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandez66284e02009-10-24 04:23:03 +0000492 CallInst* Result = NULL;
493 Value *PtrCast = Source;
494 if (InsertBefore) {
495 if (Source->getType() != IntPtrTy)
496 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
497 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
498 } else {
499 if (Source->getType() != IntPtrTy)
500 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
501 Result = CallInst::Create(FreeFunc, PtrCast, "");
502 }
503 Result->setTailCall();
Chris Lattner47fc9d32009-11-09 07:12:01 +0000504 if (Function *F = dyn_cast<Function>(FreeFunc))
505 Result->setCallingConv(F->getCallingConv());
Victor Hernandez66284e02009-10-24 04:23:03 +0000506
507 return Result;
508}
509
510/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner5a30a852010-07-12 00:57:28 +0000511Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
512 return createFree(Source, InsertBefore, NULL);
Victor Hernandez66284e02009-10-24 04:23:03 +0000513}
514
515/// CreateFree - Generate the IR for a call to the builtin free function.
516/// Note: This function does not add the call to the basic block, that is the
517/// responsibility of the caller.
518Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
519 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
520 assert(FreeCall && "CreateFree did not create a CallInst");
521 return FreeCall;
522}
523
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000524//===----------------------------------------------------------------------===//
525// InvokeInst Implementation
526//===----------------------------------------------------------------------===//
527
528void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +0000529 ArrayRef<Value *> Args, const Twine &NameStr) {
530 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifc9f75002010-03-24 13:21:49 +0000531 Op<-3>() = Fn;
532 Op<-2>() = IfNormal;
533 Op<-1>() = IfException;
Jay Foada3efbb12011-07-15 08:37:34 +0000534
535#ifndef NDEBUG
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000536 FunctionType *FTy =
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000537 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanfd939082005-04-21 23:48:37 +0000538
Jay Foada3efbb12011-07-15 08:37:34 +0000539 assert(((Args.size() == FTy->getNumParams()) ||
540 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif050560e2010-03-23 13:45:54 +0000541 "Invoking a function with bad signature");
Misha Brukmanfd939082005-04-21 23:48:37 +0000542
Jay Foada3efbb12011-07-15 08:37:34 +0000543 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner9b4c96d2006-05-03 00:48:22 +0000544 assert((i >= FTy->getNumParams() ||
Chris Lattnerd2dd1502007-02-13 01:04:01 +0000545 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner9b4c96d2006-05-03 00:48:22 +0000546 "Invoking a function with a bad signature!");
Jay Foada3efbb12011-07-15 08:37:34 +0000547#endif
548
549 std::copy(Args.begin(), Args.end(), op_begin());
550 setName(NameStr);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000551}
552
Misha Brukmanfd939082005-04-21 23:48:37 +0000553InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerb12261a2005-01-29 00:35:16 +0000554 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000555 OperandTraits<InvokeInst>::op_end(this)
556 - II.getNumOperands(),
Gabor Greifefe65362008-05-10 08:32:32 +0000557 II.getNumOperands()) {
Devang Patel05988662008-09-25 21:00:45 +0000558 setAttributes(II.getAttributes());
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000559 setCallingConv(II.getCallingConv());
Jay Foada3efbb12011-07-15 08:37:34 +0000560 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000561 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000562}
563
Chris Lattnerb12261a2005-01-29 00:35:16 +0000564BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
565 return getSuccessor(idx);
566}
567unsigned InvokeInst::getNumSuccessorsV() const {
568 return getNumSuccessors();
569}
570void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
571 return setSuccessor(idx, B);
572}
573
Bill Wendling629fb822012-12-22 00:37:52 +0000574bool InvokeInst::hasFnAttr(Attribute::AttrKind A) const {
Bill Wendling94e94b32012-12-30 13:50:49 +0000575 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling060f20a2012-10-09 00:28:54 +0000576 return true;
577 if (const Function *F = getCalledFunction())
Bill Wendling39cd0c82012-12-30 12:45:13 +0000578 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling060f20a2012-10-09 00:28:54 +0000579 return false;
580}
581
Bill Wendling629fb822012-12-22 00:37:52 +0000582bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling94e94b32012-12-30 13:50:49 +0000583 if (AttributeList.hasAttribute(i, A))
Bill Wendling847d1652012-10-03 17:54:26 +0000584 return true;
585 if (const Function *F = getCalledFunction())
Bill Wendling39cd0c82012-12-30 12:45:13 +0000586 return F->getAttributes().hasAttribute(i, A);
Bill Wendling365b9e02012-10-04 07:18:12 +0000587 return false;
588}
589
Bill Wendling034b94b2012-12-19 07:18:57 +0000590void InvokeInst::addAttribute(unsigned i, Attribute attr) {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000591 AttributeSet PAL = getAttributes();
Bill Wendlingc4167952012-10-14 07:35:59 +0000592 PAL = PAL.addAttr(getContext(), i, attr);
Devang Patel05988662008-09-25 21:00:45 +0000593 setAttributes(PAL);
Eric Christopher0bf7b412008-05-16 20:39:43 +0000594}
595
Bill Wendling034b94b2012-12-19 07:18:57 +0000596void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000597 AttributeSet PAL = getAttributes();
Bill Wendling5886b7b2012-10-14 06:39:53 +0000598 PAL = PAL.removeAttr(getContext(), i, attr);
Devang Patel05988662008-09-25 21:00:45 +0000599 setAttributes(PAL);
Duncan Sandsf0c33542007-12-19 21:13:37 +0000600}
601
Bill Wendlinge6e88262011-08-12 20:24:12 +0000602LandingPadInst *InvokeInst::getLandingPadInst() const {
603 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
604}
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000605
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000606//===----------------------------------------------------------------------===//
607// ReturnInst Implementation
608//===----------------------------------------------------------------------===//
609
Chris Lattner910c80a2007-02-24 00:55:48 +0000610ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson1d0be152009-08-13 21:58:54 +0000611 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000612 OperandTraits<ReturnInst>::op_end(this) -
613 RI.getNumOperands(),
Gabor Greifefe65362008-05-10 08:32:32 +0000614 RI.getNumOperands()) {
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000615 if (RI.getNumOperands())
Gabor Greif6c80c382008-05-26 21:33:52 +0000616 Op<0>() = RI.Op<0>();
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000617 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner910c80a2007-02-24 00:55:48 +0000618}
619
Owen Anderson1d0be152009-08-13 21:58:54 +0000620ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
621 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000622 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
623 InsertBefore) {
Devang Patel814ebd72008-02-26 18:49:29 +0000624 if (retVal)
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000625 Op<0>() = retVal;
Chris Lattner910c80a2007-02-24 00:55:48 +0000626}
Owen Anderson1d0be152009-08-13 21:58:54 +0000627ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
628 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000629 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
630 InsertAtEnd) {
Devang Patel814ebd72008-02-26 18:49:29 +0000631 if (retVal)
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000632 Op<0>() = retVal;
Chris Lattner910c80a2007-02-24 00:55:48 +0000633}
Owen Anderson1d0be152009-08-13 21:58:54 +0000634ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
635 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000636 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel57ef4f42008-02-23 00:35:18 +0000637}
638
Chris Lattnerb12261a2005-01-29 00:35:16 +0000639unsigned ReturnInst::getNumSuccessorsV() const {
640 return getNumSuccessors();
641}
642
Devang Patel64d4e612008-02-26 17:56:20 +0000643/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
644/// emit the vtable for the class in this translation unit.
Chris Lattnerb12261a2005-01-29 00:35:16 +0000645void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000646 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000647}
648
Chris Lattnerb12261a2005-01-29 00:35:16 +0000649BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000650 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerb12261a2005-01-29 00:35:16 +0000651}
652
Devang Patel57ef4f42008-02-23 00:35:18 +0000653ReturnInst::~ReturnInst() {
Devang Patel57ef4f42008-02-23 00:35:18 +0000654}
Chris Lattnerb12261a2005-01-29 00:35:16 +0000655
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000656//===----------------------------------------------------------------------===//
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000657// ResumeInst Implementation
658//===----------------------------------------------------------------------===//
659
660ResumeInst::ResumeInst(const ResumeInst &RI)
661 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
662 OperandTraits<ResumeInst>::op_begin(this), 1) {
663 Op<0>() = RI.Op<0>();
664}
665
666ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
667 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
668 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
669 Op<0>() = Exn;
670}
671
672ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
673 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
674 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
675 Op<0>() = Exn;
676}
677
678unsigned ResumeInst::getNumSuccessorsV() const {
679 return getNumSuccessors();
680}
681
682void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
683 llvm_unreachable("ResumeInst has no successors!");
684}
685
686BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
687 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000688}
689
690//===----------------------------------------------------------------------===//
Chris Lattnerb976e662004-10-16 18:08:06 +0000691// UnreachableInst Implementation
692//===----------------------------------------------------------------------===//
693
Owen Anderson1d0be152009-08-13 21:58:54 +0000694UnreachableInst::UnreachableInst(LLVMContext &Context,
695 Instruction *InsertBefore)
696 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
697 0, 0, InsertBefore) {
Chris Lattner910c80a2007-02-24 00:55:48 +0000698}
Owen Anderson1d0be152009-08-13 21:58:54 +0000699UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
700 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
701 0, 0, InsertAtEnd) {
Chris Lattner910c80a2007-02-24 00:55:48 +0000702}
703
Chris Lattnerb12261a2005-01-29 00:35:16 +0000704unsigned UnreachableInst::getNumSuccessorsV() const {
705 return getNumSuccessors();
706}
707
708void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling8833ef02012-02-06 21:44:22 +0000709 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerb12261a2005-01-29 00:35:16 +0000710}
711
712BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling8833ef02012-02-06 21:44:22 +0000713 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerb976e662004-10-16 18:08:06 +0000714}
715
716//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000717// BranchInst Implementation
718//===----------------------------------------------------------------------===//
719
Chris Lattnerb12261a2005-01-29 00:35:16 +0000720void BranchInst::AssertOK() {
721 if (isConditional())
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000722 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerb12261a2005-01-29 00:35:16 +0000723 "May only branch on boolean predicates!");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000724}
725
Chris Lattner910c80a2007-02-24 00:55:48 +0000726BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson1d0be152009-08-13 21:58:54 +0000727 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greifefe65362008-05-10 08:32:32 +0000728 OperandTraits<BranchInst>::op_end(this) - 1,
729 1, InsertBefore) {
Chris Lattner910c80a2007-02-24 00:55:48 +0000730 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifae5a20a2009-03-12 18:34:49 +0000731 Op<-1>() = IfTrue;
Chris Lattner910c80a2007-02-24 00:55:48 +0000732}
733BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
734 Instruction *InsertBefore)
Owen Anderson1d0be152009-08-13 21:58:54 +0000735 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greifefe65362008-05-10 08:32:32 +0000736 OperandTraits<BranchInst>::op_end(this) - 3,
737 3, InsertBefore) {
Gabor Greifae5a20a2009-03-12 18:34:49 +0000738 Op<-1>() = IfTrue;
739 Op<-2>() = IfFalse;
740 Op<-3>() = Cond;
Chris Lattner910c80a2007-02-24 00:55:48 +0000741#ifndef NDEBUG
742 AssertOK();
743#endif
744}
745
746BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson1d0be152009-08-13 21:58:54 +0000747 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greifefe65362008-05-10 08:32:32 +0000748 OperandTraits<BranchInst>::op_end(this) - 1,
749 1, InsertAtEnd) {
Chris Lattner910c80a2007-02-24 00:55:48 +0000750 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifae5a20a2009-03-12 18:34:49 +0000751 Op<-1>() = IfTrue;
Chris Lattner910c80a2007-02-24 00:55:48 +0000752}
753
754BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
755 BasicBlock *InsertAtEnd)
Owen Anderson1d0be152009-08-13 21:58:54 +0000756 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greifefe65362008-05-10 08:32:32 +0000757 OperandTraits<BranchInst>::op_end(this) - 3,
758 3, InsertAtEnd) {
Gabor Greifae5a20a2009-03-12 18:34:49 +0000759 Op<-1>() = IfTrue;
760 Op<-2>() = IfFalse;
761 Op<-3>() = Cond;
Chris Lattner910c80a2007-02-24 00:55:48 +0000762#ifndef NDEBUG
763 AssertOK();
764#endif
765}
766
767
Chris Lattnerb12261a2005-01-29 00:35:16 +0000768BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson1d0be152009-08-13 21:58:54 +0000769 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greifefe65362008-05-10 08:32:32 +0000770 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
771 BI.getNumOperands()) {
Gabor Greifae5a20a2009-03-12 18:34:49 +0000772 Op<-1>() = BI.Op<-1>();
Chris Lattnerb12261a2005-01-29 00:35:16 +0000773 if (BI.getNumOperands() != 1) {
774 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifae5a20a2009-03-12 18:34:49 +0000775 Op<-3>() = BI.Op<-3>();
776 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000777 }
Dan Gohman58cfa3b2009-08-25 22:11:20 +0000778 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000779}
780
Chandler Carruth602650c2011-10-17 01:11:57 +0000781void BranchInst::swapSuccessors() {
782 assert(isConditional() &&
783 "Cannot swap successors of an unconditional branch");
784 Op<-1>().swap(Op<-2>());
785
786 // Update profile metadata if present and it matches our structural
787 // expectations.
788 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
789 if (!ProfileData || ProfileData->getNumOperands() != 3)
790 return;
791
792 // The first operand is the name. Fetch them backwards and build a new one.
793 Value *Ops[] = {
794 ProfileData->getOperand(0),
795 ProfileData->getOperand(2),
796 ProfileData->getOperand(1)
797 };
798 setMetadata(LLVMContext::MD_prof,
799 MDNode::get(ProfileData->getContext(), Ops));
800}
801
Chris Lattnerb12261a2005-01-29 00:35:16 +0000802BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
803 return getSuccessor(idx);
804}
805unsigned BranchInst::getNumSuccessorsV() const {
806 return getNumSuccessors();
807}
808void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
809 setSuccessor(idx, B);
810}
811
812
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000813//===----------------------------------------------------------------------===//
Victor Hernandez7b929da2009-10-23 21:09:37 +0000814// AllocaInst Implementation
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000815//===----------------------------------------------------------------------===//
816
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000817static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerb12261a2005-01-29 00:35:16 +0000818 if (!Amt)
Owen Anderson1d0be152009-08-13 21:58:54 +0000819 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnere46749c2006-05-10 04:32:43 +0000820 else {
821 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner53336cb2007-10-18 16:10:48 +0000822 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohmanf75a7d32010-05-28 01:14:11 +0000823 assert(Amt->getType()->isIntegerTy() &&
824 "Allocation array size is not an integer!");
Chris Lattnere46749c2006-05-10 04:32:43 +0000825 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000826 return Amt;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000827}
828
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000829AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez7b929da2009-10-23 21:09:37 +0000830 const Twine &Name, Instruction *InsertBefore)
831 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
832 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
833 setAlignment(0);
Benjamin Kramerf0127052010-01-05 13:12:22 +0000834 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez7b929da2009-10-23 21:09:37 +0000835 setName(Name);
836}
837
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000838AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez7b929da2009-10-23 21:09:37 +0000839 const Twine &Name, BasicBlock *InsertAtEnd)
840 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
841 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
842 setAlignment(0);
Benjamin Kramerf0127052010-01-05 13:12:22 +0000843 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez7b929da2009-10-23 21:09:37 +0000844 setName(Name);
845}
846
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000847AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez7b929da2009-10-23 21:09:37 +0000848 Instruction *InsertBefore)
849 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
850 getAISize(Ty->getContext(), 0), InsertBefore) {
851 setAlignment(0);
Benjamin Kramerf0127052010-01-05 13:12:22 +0000852 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez7b929da2009-10-23 21:09:37 +0000853 setName(Name);
854}
855
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000856AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez7b929da2009-10-23 21:09:37 +0000857 BasicBlock *InsertAtEnd)
858 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
859 getAISize(Ty->getContext(), 0), InsertAtEnd) {
860 setAlignment(0);
Benjamin Kramerf0127052010-01-05 13:12:22 +0000861 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez7b929da2009-10-23 21:09:37 +0000862 setName(Name);
863}
864
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000865AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez7b929da2009-10-23 21:09:37 +0000866 const Twine &Name, Instruction *InsertBefore)
867 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson50dead02009-07-15 23:53:25 +0000868 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohman52837072008-03-24 16:55:58 +0000869 setAlignment(Align);
Benjamin Kramerf0127052010-01-05 13:12:22 +0000870 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattnerf00042a2007-02-13 07:54:42 +0000871 setName(Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000872}
873
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000874AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez7b929da2009-10-23 21:09:37 +0000875 const Twine &Name, BasicBlock *InsertAtEnd)
876 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson50dead02009-07-15 23:53:25 +0000877 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohman52837072008-03-24 16:55:58 +0000878 setAlignment(Align);
Benjamin Kramerf0127052010-01-05 13:12:22 +0000879 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattnerf00042a2007-02-13 07:54:42 +0000880 setName(Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000881}
882
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000883// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez7b929da2009-10-23 21:09:37 +0000884AllocaInst::~AllocaInst() {
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000885}
886
Victor Hernandez7b929da2009-10-23 21:09:37 +0000887void AllocaInst::setAlignment(unsigned Align) {
Dan Gohman52837072008-03-24 16:55:58 +0000888 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohman138aa2a2010-07-28 20:12:04 +0000889 assert(Align <= MaximumAlignment &&
890 "Alignment is greater than MaximumAlignment!");
Chris Lattnerb2406d92009-12-29 02:46:09 +0000891 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohman52837072008-03-24 16:55:58 +0000892 assert(getAlignment() == Align && "Alignment representation error!");
893}
894
Victor Hernandez7b929da2009-10-23 21:09:37 +0000895bool AllocaInst::isArrayAllocation() const {
Reid Spencerbb9723b2007-03-01 20:27:41 +0000896 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohmane0214d52010-09-27 15:15:44 +0000897 return !CI->isOne();
Chris Lattnerb12261a2005-01-29 00:35:16 +0000898 return true;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000899}
900
Chris Lattner1afcace2011-07-09 17:41:24 +0000901Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000902 return getType()->getElementType();
903}
904
Chris Lattnerc5dd22a2008-11-26 02:54:17 +0000905/// isStaticAlloca - Return true if this alloca is in the entry block of the
906/// function and is a constant size. If so, the code generator will fold it
907/// into the prolog/epilog code, so it is basically free.
908bool AllocaInst::isStaticAlloca() const {
909 // Must be constant size.
910 if (!isa<ConstantInt>(getArraySize())) return false;
911
912 // Must be in the entry block.
913 const BasicBlock *Parent = getParent();
914 return Parent == &Parent->getParent()->front();
915}
916
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000917//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000918// LoadInst Implementation
919//===----------------------------------------------------------------------===//
920
Chris Lattnerb12261a2005-01-29 00:35:16 +0000921void LoadInst::AssertOK() {
Duncan Sands1df98592010-02-16 11:11:14 +0000922 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000923 "Ptr must have pointer type.");
Eli Friedman21006d42011-08-09 23:02:53 +0000924 assert(!(isAtomic() && getAlignment() == 0) &&
925 "Alignment required for atomic load");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000926}
927
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000928LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerb12261a2005-01-29 00:35:16 +0000929 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner910c80a2007-02-24 00:55:48 +0000930 Load, Ptr, InsertBef) {
Chris Lattner28662972005-02-05 01:38:38 +0000931 setVolatile(false);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000932 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +0000933 setAtomic(NotAtomic);
Chris Lattnerb12261a2005-01-29 00:35:16 +0000934 AssertOK();
Chris Lattnerf00042a2007-02-13 07:54:42 +0000935 setName(Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000936}
937
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000938LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerb12261a2005-01-29 00:35:16 +0000939 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner910c80a2007-02-24 00:55:48 +0000940 Load, Ptr, InsertAE) {
Chris Lattner28662972005-02-05 01:38:38 +0000941 setVolatile(false);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000942 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +0000943 setAtomic(NotAtomic);
Chris Lattnerb12261a2005-01-29 00:35:16 +0000944 AssertOK();
Chris Lattnerf00042a2007-02-13 07:54:42 +0000945 setName(Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000946}
947
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000948LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000949 Instruction *InsertBef)
Chris Lattnerb12261a2005-01-29 00:35:16 +0000950 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner910c80a2007-02-24 00:55:48 +0000951 Load, Ptr, InsertBef) {
Chris Lattner28662972005-02-05 01:38:38 +0000952 setVolatile(isVolatile);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000953 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +0000954 setAtomic(NotAtomic);
955 AssertOK();
956 setName(Name);
957}
958
959LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
960 BasicBlock *InsertAE)
961 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
962 Load, Ptr, InsertAE) {
963 setVolatile(isVolatile);
964 setAlignment(0);
965 setAtomic(NotAtomic);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000966 AssertOK();
967 setName(Name);
968}
969
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000970LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000971 unsigned Align, Instruction *InsertBef)
972 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
973 Load, Ptr, InsertBef) {
974 setVolatile(isVolatile);
975 setAlignment(Align);
Eli Friedman21006d42011-08-09 23:02:53 +0000976 setAtomic(NotAtomic);
Chris Lattnerb12261a2005-01-29 00:35:16 +0000977 AssertOK();
Chris Lattnerf00042a2007-02-13 07:54:42 +0000978 setName(Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +0000979}
980
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000981LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman6ab2d182007-07-18 20:51:11 +0000982 unsigned Align, BasicBlock *InsertAE)
983 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
984 Load, Ptr, InsertAE) {
985 setVolatile(isVolatile);
986 setAlignment(Align);
Eli Friedman21006d42011-08-09 23:02:53 +0000987 setAtomic(NotAtomic);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000988 AssertOK();
989 setName(Name);
990}
991
Eli Friedman21006d42011-08-09 23:02:53 +0000992LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
993 unsigned Align, AtomicOrdering Order,
994 SynchronizationScope SynchScope,
995 Instruction *InsertBef)
996 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
997 Load, Ptr, InsertBef) {
998 setVolatile(isVolatile);
999 setAlignment(Align);
1000 setAtomic(Order, SynchScope);
1001 AssertOK();
1002 setName(Name);
1003}
1004
1005LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1006 unsigned Align, AtomicOrdering Order,
1007 SynchronizationScope SynchScope,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001008 BasicBlock *InsertAE)
Chris Lattnerb12261a2005-01-29 00:35:16 +00001009 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner910c80a2007-02-24 00:55:48 +00001010 Load, Ptr, InsertAE) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001011 setVolatile(isVolatile);
Eli Friedman21006d42011-08-09 23:02:53 +00001012 setAlignment(Align);
1013 setAtomic(Order, SynchScope);
Chris Lattnerf00042a2007-02-13 07:54:42 +00001014 AssertOK();
1015 setName(Name);
1016}
1017
Daniel Dunbar3603d7a2009-08-11 18:11:15 +00001018LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1019 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1020 Load, Ptr, InsertBef) {
1021 setVolatile(false);
1022 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +00001023 setAtomic(NotAtomic);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +00001024 AssertOK();
1025 if (Name && Name[0]) setName(Name);
1026}
1027
1028LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1029 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1030 Load, Ptr, InsertAE) {
1031 setVolatile(false);
1032 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +00001033 setAtomic(NotAtomic);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +00001034 AssertOK();
1035 if (Name && Name[0]) setName(Name);
1036}
1037
1038LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1039 Instruction *InsertBef)
1040: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1041 Load, Ptr, InsertBef) {
1042 setVolatile(isVolatile);
1043 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +00001044 setAtomic(NotAtomic);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +00001045 AssertOK();
1046 if (Name && Name[0]) setName(Name);
1047}
1048
1049LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1050 BasicBlock *InsertAE)
1051 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1052 Load, Ptr, InsertAE) {
1053 setVolatile(isVolatile);
1054 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +00001055 setAtomic(NotAtomic);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +00001056 AssertOK();
1057 if (Name && Name[0]) setName(Name);
1058}
1059
Christopher Lamb43c7f372007-04-22 19:24:39 +00001060void LoadInst::setAlignment(unsigned Align) {
1061 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohman138aa2a2010-07-28 20:12:04 +00001062 assert(Align <= MaximumAlignment &&
1063 "Alignment is greater than MaximumAlignment!");
Eli Friedman21006d42011-08-09 23:02:53 +00001064 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerb2406d92009-12-29 02:46:09 +00001065 ((Log2_32(Align)+1)<<1));
Dan Gohman138aa2a2010-07-28 20:12:04 +00001066 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb43c7f372007-04-22 19:24:39 +00001067}
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001068
1069//===----------------------------------------------------------------------===//
1070// StoreInst Implementation
1071//===----------------------------------------------------------------------===//
1072
Chris Lattnerb12261a2005-01-29 00:35:16 +00001073void StoreInst::AssertOK() {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00001074 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands1df98592010-02-16 11:11:14 +00001075 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerb12261a2005-01-29 00:35:16 +00001076 "Ptr must have pointer type!");
1077 assert(getOperand(0)->getType() ==
1078 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos8fabb622004-08-06 14:33:37 +00001079 && "Ptr must be a pointer to Val type!");
Eli Friedman21006d42011-08-09 23:02:53 +00001080 assert(!(isAtomic() && getAlignment() == 0) &&
1081 "Alignment required for atomic load");
Chris Lattnerb12261a2005-01-29 00:35:16 +00001082}
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001083
Chris Lattnerb12261a2005-01-29 00:35:16 +00001084
1085StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson1d0be152009-08-13 21:58:54 +00001086 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greifefe65362008-05-10 08:32:32 +00001087 OperandTraits<StoreInst>::op_begin(this),
1088 OperandTraits<StoreInst>::operands(this),
1089 InsertBefore) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001090 Op<0>() = val;
1091 Op<1>() = addr;
Chris Lattner28662972005-02-05 01:38:38 +00001092 setVolatile(false);
Christopher Lamb43c7f372007-04-22 19:24:39 +00001093 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +00001094 setAtomic(NotAtomic);
Chris Lattnerb12261a2005-01-29 00:35:16 +00001095 AssertOK();
1096}
1097
1098StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson1d0be152009-08-13 21:58:54 +00001099 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greifefe65362008-05-10 08:32:32 +00001100 OperandTraits<StoreInst>::op_begin(this),
1101 OperandTraits<StoreInst>::operands(this),
1102 InsertAtEnd) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001103 Op<0>() = val;
1104 Op<1>() = addr;
Chris Lattner28662972005-02-05 01:38:38 +00001105 setVolatile(false);
Christopher Lamb43c7f372007-04-22 19:24:39 +00001106 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +00001107 setAtomic(NotAtomic);
Chris Lattnerb12261a2005-01-29 00:35:16 +00001108 AssertOK();
1109}
1110
Misha Brukmanfd939082005-04-21 23:48:37 +00001111StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerb12261a2005-01-29 00:35:16 +00001112 Instruction *InsertBefore)
Owen Anderson1d0be152009-08-13 21:58:54 +00001113 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greifefe65362008-05-10 08:32:32 +00001114 OperandTraits<StoreInst>::op_begin(this),
1115 OperandTraits<StoreInst>::operands(this),
1116 InsertBefore) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001117 Op<0>() = val;
1118 Op<1>() = addr;
Chris Lattner28662972005-02-05 01:38:38 +00001119 setVolatile(isVolatile);
Christopher Lamb43c7f372007-04-22 19:24:39 +00001120 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +00001121 setAtomic(NotAtomic);
Christopher Lamb43c7f372007-04-22 19:24:39 +00001122 AssertOK();
1123}
1124
1125StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1126 unsigned Align, Instruction *InsertBefore)
Owen Anderson1d0be152009-08-13 21:58:54 +00001127 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greifefe65362008-05-10 08:32:32 +00001128 OperandTraits<StoreInst>::op_begin(this),
1129 OperandTraits<StoreInst>::operands(this),
1130 InsertBefore) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001131 Op<0>() = val;
1132 Op<1>() = addr;
Christopher Lamb43c7f372007-04-22 19:24:39 +00001133 setVolatile(isVolatile);
1134 setAlignment(Align);
Eli Friedman21006d42011-08-09 23:02:53 +00001135 setAtomic(NotAtomic);
Chris Lattnerb12261a2005-01-29 00:35:16 +00001136 AssertOK();
1137}
1138
Misha Brukmanfd939082005-04-21 23:48:37 +00001139StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +00001140 unsigned Align, AtomicOrdering Order,
1141 SynchronizationScope SynchScope,
1142 Instruction *InsertBefore)
Owen Anderson1d0be152009-08-13 21:58:54 +00001143 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greifefe65362008-05-10 08:32:32 +00001144 OperandTraits<StoreInst>::op_begin(this),
1145 OperandTraits<StoreInst>::operands(this),
Eli Friedman21006d42011-08-09 23:02:53 +00001146 InsertBefore) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001147 Op<0>() = val;
1148 Op<1>() = addr;
Dan Gohman6ab2d182007-07-18 20:51:11 +00001149 setVolatile(isVolatile);
1150 setAlignment(Align);
Eli Friedman21006d42011-08-09 23:02:53 +00001151 setAtomic(Order, SynchScope);
Dan Gohman6ab2d182007-07-18 20:51:11 +00001152 AssertOK();
1153}
1154
1155StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerb12261a2005-01-29 00:35:16 +00001156 BasicBlock *InsertAtEnd)
Owen Anderson1d0be152009-08-13 21:58:54 +00001157 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greifefe65362008-05-10 08:32:32 +00001158 OperandTraits<StoreInst>::op_begin(this),
1159 OperandTraits<StoreInst>::operands(this),
1160 InsertAtEnd) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001161 Op<0>() = val;
1162 Op<1>() = addr;
Chris Lattner28662972005-02-05 01:38:38 +00001163 setVolatile(isVolatile);
Christopher Lamb43c7f372007-04-22 19:24:39 +00001164 setAlignment(0);
Eli Friedman21006d42011-08-09 23:02:53 +00001165 setAtomic(NotAtomic);
1166 AssertOK();
1167}
1168
1169StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1170 unsigned Align, BasicBlock *InsertAtEnd)
1171 : Instruction(Type::getVoidTy(val->getContext()), Store,
1172 OperandTraits<StoreInst>::op_begin(this),
1173 OperandTraits<StoreInst>::operands(this),
1174 InsertAtEnd) {
1175 Op<0>() = val;
1176 Op<1>() = addr;
1177 setVolatile(isVolatile);
1178 setAlignment(Align);
1179 setAtomic(NotAtomic);
1180 AssertOK();
1181}
1182
1183StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1184 unsigned Align, AtomicOrdering Order,
1185 SynchronizationScope SynchScope,
1186 BasicBlock *InsertAtEnd)
1187 : Instruction(Type::getVoidTy(val->getContext()), Store,
1188 OperandTraits<StoreInst>::op_begin(this),
1189 OperandTraits<StoreInst>::operands(this),
1190 InsertAtEnd) {
1191 Op<0>() = val;
1192 Op<1>() = addr;
1193 setVolatile(isVolatile);
1194 setAlignment(Align);
1195 setAtomic(Order, SynchScope);
Chris Lattnerb12261a2005-01-29 00:35:16 +00001196 AssertOK();
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001197}
1198
Christopher Lamb43c7f372007-04-22 19:24:39 +00001199void StoreInst::setAlignment(unsigned Align) {
1200 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohman138aa2a2010-07-28 20:12:04 +00001201 assert(Align <= MaximumAlignment &&
1202 "Alignment is greater than MaximumAlignment!");
Eli Friedman21006d42011-08-09 23:02:53 +00001203 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerb2406d92009-12-29 02:46:09 +00001204 ((Log2_32(Align)+1) << 1));
Dan Gohman138aa2a2010-07-28 20:12:04 +00001205 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb43c7f372007-04-22 19:24:39 +00001206}
1207
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001208//===----------------------------------------------------------------------===//
Eli Friedmanff030482011-07-28 21:48:00 +00001209// AtomicCmpXchgInst Implementation
1210//===----------------------------------------------------------------------===//
1211
1212void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1213 AtomicOrdering Ordering,
1214 SynchronizationScope SynchScope) {
1215 Op<0>() = Ptr;
1216 Op<1>() = Cmp;
1217 Op<2>() = NewVal;
1218 setOrdering(Ordering);
1219 setSynchScope(SynchScope);
1220
1221 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1222 "All operands must be non-null!");
1223 assert(getOperand(0)->getType()->isPointerTy() &&
1224 "Ptr must have pointer type!");
1225 assert(getOperand(1)->getType() ==
1226 cast<PointerType>(getOperand(0)->getType())->getElementType()
1227 && "Ptr must be a pointer to Cmp type!");
1228 assert(getOperand(2)->getType() ==
1229 cast<PointerType>(getOperand(0)->getType())->getElementType()
1230 && "Ptr must be a pointer to NewVal type!");
1231 assert(Ordering != NotAtomic &&
1232 "AtomicCmpXchg instructions must be atomic!");
1233}
1234
1235AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1236 AtomicOrdering Ordering,
1237 SynchronizationScope SynchScope,
1238 Instruction *InsertBefore)
1239 : Instruction(Cmp->getType(), AtomicCmpXchg,
1240 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1241 OperandTraits<AtomicCmpXchgInst>::operands(this),
1242 InsertBefore) {
1243 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1244}
1245
1246AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1247 AtomicOrdering Ordering,
1248 SynchronizationScope SynchScope,
1249 BasicBlock *InsertAtEnd)
1250 : Instruction(Cmp->getType(), AtomicCmpXchg,
1251 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1252 OperandTraits<AtomicCmpXchgInst>::operands(this),
1253 InsertAtEnd) {
1254 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1255}
1256
1257//===----------------------------------------------------------------------===//
1258// AtomicRMWInst Implementation
1259//===----------------------------------------------------------------------===//
1260
1261void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1262 AtomicOrdering Ordering,
1263 SynchronizationScope SynchScope) {
1264 Op<0>() = Ptr;
1265 Op<1>() = Val;
1266 setOperation(Operation);
1267 setOrdering(Ordering);
1268 setSynchScope(SynchScope);
1269
1270 assert(getOperand(0) && getOperand(1) &&
1271 "All operands must be non-null!");
1272 assert(getOperand(0)->getType()->isPointerTy() &&
1273 "Ptr must have pointer type!");
1274 assert(getOperand(1)->getType() ==
1275 cast<PointerType>(getOperand(0)->getType())->getElementType()
1276 && "Ptr must be a pointer to Val type!");
1277 assert(Ordering != NotAtomic &&
1278 "AtomicRMW instructions must be atomic!");
1279}
1280
1281AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1282 AtomicOrdering Ordering,
1283 SynchronizationScope SynchScope,
1284 Instruction *InsertBefore)
1285 : Instruction(Val->getType(), AtomicRMW,
1286 OperandTraits<AtomicRMWInst>::op_begin(this),
1287 OperandTraits<AtomicRMWInst>::operands(this),
1288 InsertBefore) {
1289 Init(Operation, Ptr, Val, Ordering, SynchScope);
1290}
1291
1292AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1293 AtomicOrdering Ordering,
1294 SynchronizationScope SynchScope,
1295 BasicBlock *InsertAtEnd)
1296 : Instruction(Val->getType(), AtomicRMW,
1297 OperandTraits<AtomicRMWInst>::op_begin(this),
1298 OperandTraits<AtomicRMWInst>::operands(this),
1299 InsertAtEnd) {
1300 Init(Operation, Ptr, Val, Ordering, SynchScope);
1301}
1302
1303//===----------------------------------------------------------------------===//
Eli Friedman47f35132011-07-25 23:16:38 +00001304// FenceInst Implementation
1305//===----------------------------------------------------------------------===//
1306
1307FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1308 SynchronizationScope SynchScope,
1309 Instruction *InsertBefore)
1310 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1311 setOrdering(Ordering);
1312 setSynchScope(SynchScope);
1313}
1314
1315FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1316 SynchronizationScope SynchScope,
1317 BasicBlock *InsertAtEnd)
1318 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1319 setOrdering(Ordering);
1320 setSynchScope(SynchScope);
1321}
1322
1323//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001324// GetElementPtrInst Implementation
1325//===----------------------------------------------------------------------===//
1326
Jay Foada9203102011-07-25 09:48:08 +00001327void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001328 const Twine &Name) {
Jay Foada9203102011-07-25 09:48:08 +00001329 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1330 OperandList[0] = Ptr;
1331 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman338169d2008-06-04 16:14:12 +00001332 setName(Name);
Chris Lattner38bacf22005-05-03 05:43:30 +00001333}
1334
Gabor Greifefe65362008-05-10 08:32:32 +00001335GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greifb9d9eba2008-05-27 11:03:29 +00001336 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greifb1dbcd82008-05-15 10:04:30 +00001337 OperandTraits<GetElementPtrInst>::op_end(this)
1338 - GEPI.getNumOperands(),
Gabor Greifefe65362008-05-10 08:32:32 +00001339 GEPI.getNumOperands()) {
Jay Foada9203102011-07-25 09:48:08 +00001340 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohman58cfa3b2009-08-25 22:11:20 +00001341 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greifefe65362008-05-10 08:32:32 +00001342}
1343
Chris Lattnerc66996a2009-03-09 04:46:40 +00001344/// getIndexedType - Returns the type of the element that would be accessed with
1345/// a gep instruction with the specified parameters.
1346///
1347/// The Idxs pointer should point to a continuous piece of memory containing the
1348/// indices, either as Value* or uint64_t.
1349///
1350/// A null type is returned if the indices are invalid for the specified
1351/// pointer type.
1352///
Matthijs Kooijmane2afded2008-07-29 08:46:11 +00001353template <typename IndexTy>
Jay Foada9203102011-07-25 09:48:08 +00001354static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Duncan Sands2333e292012-11-13 12:59:33 +00001355 PointerType *PTy = dyn_cast<PointerType>(Ptr->getScalarType());
Dan Gohman041e2eb2008-05-15 19:50:34 +00001356 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattner1afcace2011-07-09 17:41:24 +00001357 Type *Agg = PTy->getElementType();
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001358
Chris Lattnerc66996a2009-03-09 04:46:40 +00001359 // Handle the special case of the empty set index set, which is always valid.
Jay Foada9203102011-07-25 09:48:08 +00001360 if (IdxList.empty())
Dan Gohman041e2eb2008-05-15 19:50:34 +00001361 return Agg;
Nadav Rotem16087692011-12-05 06:29:09 +00001362
Chris Lattnerc66996a2009-03-09 04:46:40 +00001363 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner1afcace2011-07-09 17:41:24 +00001364 // it cannot be 'stepped over'.
1365 if (!Agg->isSized())
Chris Lattnerc66996a2009-03-09 04:46:40 +00001366 return 0;
Misha Brukmanfd939082005-04-21 23:48:37 +00001367
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001368 unsigned CurIdx = 1;
Jay Foada9203102011-07-25 09:48:08 +00001369 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattner1afcace2011-07-09 17:41:24 +00001370 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands1df98592010-02-16 11:11:14 +00001371 if (!CT || CT->isPointerTy()) return 0;
Jay Foada9203102011-07-25 09:48:08 +00001372 IndexTy Index = IdxList[CurIdx];
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001373 if (!CT->indexValid(Index)) return 0;
1374 Agg = CT->getTypeAtIndex(Index);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001375 }
Jay Foada9203102011-07-25 09:48:08 +00001376 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001377}
1378
Jay Foada9203102011-07-25 09:48:08 +00001379Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1380 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijmane2afded2008-07-29 08:46:11 +00001381}
1382
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001383Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foada9203102011-07-25 09:48:08 +00001384 ArrayRef<Constant *> IdxList) {
1385 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad25052d82011-01-14 08:07:43 +00001386}
1387
Jay Foada9203102011-07-25 09:48:08 +00001388Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1389 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijmane2afded2008-07-29 08:46:11 +00001390}
1391
Chris Lattner6f771d42007-04-14 00:12:57 +00001392/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1393/// zeros. If so, the result pointer and the first operand have the same
1394/// value, just potentially different types.
1395bool GetElementPtrInst::hasAllZeroIndices() const {
1396 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1397 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1398 if (!CI->isZero()) return false;
1399 } else {
1400 return false;
1401 }
1402 }
1403 return true;
1404}
1405
Chris Lattner6b0974c2007-04-27 20:35:56 +00001406/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1407/// constant integers. If so, the result pointer and the first operand have
1408/// a constant offset between them.
1409bool GetElementPtrInst::hasAllConstantIndices() const {
1410 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1411 if (!isa<ConstantInt>(getOperand(i)))
1412 return false;
1413 }
1414 return true;
1415}
1416
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001417void GetElementPtrInst::setIsInBounds(bool B) {
1418 cast<GEPOperator>(this)->setIsInBounds(B);
1419}
Chris Lattner6f771d42007-04-14 00:12:57 +00001420
Nick Lewyckyae05e7d2009-09-27 21:33:04 +00001421bool GetElementPtrInst::isInBounds() const {
1422 return cast<GEPOperator>(this)->isInBounds();
1423}
1424
Chandler Carruth4ced4ee2012-12-11 10:29:10 +00001425bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1426 APInt &Offset) const {
Chandler Carruth7550f962012-12-11 11:05:15 +00001427 // Delegate to the generic GEPOperator implementation.
1428 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth4ced4ee2012-12-11 10:29:10 +00001429}
1430
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001431//===----------------------------------------------------------------------===//
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001432// ExtractElementInst Implementation
1433//===----------------------------------------------------------------------===//
1434
1435ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001436 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001437 Instruction *InsertBef)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001438 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greifefe65362008-05-10 08:32:32 +00001439 ExtractElement,
1440 OperandTraits<ExtractElementInst>::op_begin(this),
1441 2, InsertBef) {
Chris Lattnerd2325d02006-04-08 04:05:48 +00001442 assert(isValidOperands(Val, Index) &&
1443 "Invalid extractelement instruction operands!");
Gabor Greif6c80c382008-05-26 21:33:52 +00001444 Op<0>() = Val;
1445 Op<1>() = Index;
Chris Lattner910c80a2007-02-24 00:55:48 +00001446 setName(Name);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001447}
1448
1449ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001450 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001451 BasicBlock *InsertAE)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001452 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greifefe65362008-05-10 08:32:32 +00001453 ExtractElement,
1454 OperandTraits<ExtractElementInst>::op_begin(this),
1455 2, InsertAE) {
Chris Lattnerd2325d02006-04-08 04:05:48 +00001456 assert(isValidOperands(Val, Index) &&
1457 "Invalid extractelement instruction operands!");
1458
Gabor Greif6c80c382008-05-26 21:33:52 +00001459 Op<0>() = Val;
1460 Op<1>() = Index;
Chris Lattner910c80a2007-02-24 00:55:48 +00001461 setName(Name);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001462}
1463
Chris Lattner06a248c22006-10-05 06:24:58 +00001464
Chris Lattnerd2325d02006-04-08 04:05:48 +00001465bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands1df98592010-02-16 11:11:14 +00001466 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattnerd2325d02006-04-08 04:05:48 +00001467 return false;
1468 return true;
1469}
1470
1471
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001472//===----------------------------------------------------------------------===//
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001473// InsertElementInst Implementation
1474//===----------------------------------------------------------------------===//
1475
Chris Lattnerd2325d02006-04-08 04:05:48 +00001476InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001477 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001478 Instruction *InsertBef)
Gabor Greifefe65362008-05-10 08:32:32 +00001479 : Instruction(Vec->getType(), InsertElement,
1480 OperandTraits<InsertElementInst>::op_begin(this),
1481 3, InsertBef) {
Chris Lattnerd2325d02006-04-08 04:05:48 +00001482 assert(isValidOperands(Vec, Elt, Index) &&
1483 "Invalid insertelement instruction operands!");
Gabor Greif6c80c382008-05-26 21:33:52 +00001484 Op<0>() = Vec;
1485 Op<1>() = Elt;
1486 Op<2>() = Index;
Chris Lattner910c80a2007-02-24 00:55:48 +00001487 setName(Name);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001488}
1489
Chris Lattnerd2325d02006-04-08 04:05:48 +00001490InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001491 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001492 BasicBlock *InsertAE)
Gabor Greifefe65362008-05-10 08:32:32 +00001493 : Instruction(Vec->getType(), InsertElement,
1494 OperandTraits<InsertElementInst>::op_begin(this),
1495 3, InsertAE) {
Chris Lattnerd2325d02006-04-08 04:05:48 +00001496 assert(isValidOperands(Vec, Elt, Index) &&
1497 "Invalid insertelement instruction operands!");
1498
Gabor Greif6c80c382008-05-26 21:33:52 +00001499 Op<0>() = Vec;
1500 Op<1>() = Elt;
1501 Op<2>() = Index;
Chris Lattner910c80a2007-02-24 00:55:48 +00001502 setName(Name);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001503}
1504
Chris Lattnerd2325d02006-04-08 04:05:48 +00001505bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1506 const Value *Index) {
Duncan Sands1df98592010-02-16 11:11:14 +00001507 if (!Vec->getType()->isVectorTy())
Reid Spencerac9dcb92007-02-15 03:39:18 +00001508 return false; // First operand of insertelement must be vector type.
Chris Lattnerd2325d02006-04-08 04:05:48 +00001509
Reid Spencer9d6565a2007-02-15 02:26:10 +00001510 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohman86296cc2007-05-11 21:43:24 +00001511 return false;// Second operand of insertelement must be vector element type.
Chris Lattnerd2325d02006-04-08 04:05:48 +00001512
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001513 if (!Index->getType()->isIntegerTy(32))
Dan Gohmana119de82009-06-14 23:30:43 +00001514 return false; // Third operand of insertelement must be i32.
Chris Lattnerd2325d02006-04-08 04:05:48 +00001515 return true;
1516}
1517
1518
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001519//===----------------------------------------------------------------------===//
Chris Lattner00f10232006-04-08 01:18:18 +00001520// ShuffleVectorInst Implementation
1521//===----------------------------------------------------------------------===//
1522
1523ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001524 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001525 Instruction *InsertBefore)
Owen Andersondebcb012009-07-29 22:17:13 +00001526: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wangaeb06d22008-11-10 04:46:22 +00001527 cast<VectorType>(Mask->getType())->getNumElements()),
1528 ShuffleVector,
1529 OperandTraits<ShuffleVectorInst>::op_begin(this),
1530 OperandTraits<ShuffleVectorInst>::operands(this),
1531 InsertBefore) {
Chris Lattner00f10232006-04-08 01:18:18 +00001532 assert(isValidOperands(V1, V2, Mask) &&
1533 "Invalid shuffle vector instruction operands!");
Gabor Greif6c80c382008-05-26 21:33:52 +00001534 Op<0>() = V1;
1535 Op<1>() = V2;
1536 Op<2>() = Mask;
Chris Lattner910c80a2007-02-24 00:55:48 +00001537 setName(Name);
Chris Lattner00f10232006-04-08 01:18:18 +00001538}
1539
1540ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001541 const Twine &Name,
Chris Lattner00f10232006-04-08 01:18:18 +00001542 BasicBlock *InsertAtEnd)
Dan Gohman638d8532009-08-25 23:27:45 +00001543: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1544 cast<VectorType>(Mask->getType())->getNumElements()),
1545 ShuffleVector,
1546 OperandTraits<ShuffleVectorInst>::op_begin(this),
1547 OperandTraits<ShuffleVectorInst>::operands(this),
1548 InsertAtEnd) {
Chris Lattner00f10232006-04-08 01:18:18 +00001549 assert(isValidOperands(V1, V2, Mask) &&
1550 "Invalid shuffle vector instruction operands!");
1551
Gabor Greif6c80c382008-05-26 21:33:52 +00001552 Op<0>() = V1;
1553 Op<1>() = V2;
1554 Op<2>() = Mask;
Chris Lattner910c80a2007-02-24 00:55:48 +00001555 setName(Name);
Chris Lattner00f10232006-04-08 01:18:18 +00001556}
1557
Mon P Wangaeb06d22008-11-10 04:46:22 +00001558bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattner00f10232006-04-08 01:18:18 +00001559 const Value *Mask) {
Chris Lattner83694a92012-01-25 23:49:49 +00001560 // V1 and V2 must be vectors of the same type.
Duncan Sands1df98592010-02-16 11:11:14 +00001561 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattner8728f192008-03-02 05:28:33 +00001562 return false;
1563
Chris Lattner83694a92012-01-25 23:49:49 +00001564 // Mask must be vector of i32.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001565 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begemana966af22010-08-13 00:16:46 +00001566 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattner00f10232006-04-08 01:18:18 +00001567 return false;
Nate Begemana966af22010-08-13 00:16:46 +00001568
1569 // Check to see if Mask is valid.
Chris Lattner83694a92012-01-25 23:49:49 +00001570 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1571 return true;
1572
Nate Begemana966af22010-08-13 00:16:46 +00001573 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner83694a92012-01-25 23:49:49 +00001574 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begemana966af22010-08-13 00:16:46 +00001575 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner83694a92012-01-25 23:49:49 +00001576 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1577 if (CI->uge(V1Size*2))
Nate Begemana966af22010-08-13 00:16:46 +00001578 return false;
1579 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1580 return false;
1581 }
1582 }
Chris Lattner83694a92012-01-25 23:49:49 +00001583 return true;
Mon P Wangcf62b372011-10-26 00:34:48 +00001584 }
Chris Lattner83694a92012-01-25 23:49:49 +00001585
1586 if (const ConstantDataSequential *CDS =
1587 dyn_cast<ConstantDataSequential>(Mask)) {
1588 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1589 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1590 if (CDS->getElementAsInteger(i) >= V1Size*2)
1591 return false;
1592 return true;
1593 }
1594
1595 // The bitcode reader can create a place holder for a forward reference
1596 // used as the shuffle mask. When this occurs, the shuffle mask will
1597 // fall into this case and fail. To avoid this error, do this bit of
1598 // ugliness to allow such a mask pass.
1599 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1600 if (CE->getOpcode() == Instruction::UserOp1)
1601 return true;
1602
1603 return false;
Chris Lattner00f10232006-04-08 01:18:18 +00001604}
1605
Chris Lattner8728f192008-03-02 05:28:33 +00001606/// getMaskValue - Return the index from the shuffle mask for the specified
1607/// output result. This is either -1 if the element is undef or a number less
1608/// than 2*numelements.
Chris Lattner56243b82012-01-26 02:51:13 +00001609int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1610 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1611 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner83694a92012-01-25 23:49:49 +00001612 return CDS->getElementAsInteger(i);
Chris Lattner56243b82012-01-26 02:51:13 +00001613 Constant *C = Mask->getAggregateElement(i);
Chris Lattner83694a92012-01-25 23:49:49 +00001614 if (isa<UndefValue>(C))
Chris Lattner8728f192008-03-02 05:28:33 +00001615 return -1;
Chris Lattner83694a92012-01-25 23:49:49 +00001616 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattner8728f192008-03-02 05:28:33 +00001617}
1618
Chris Lattner83694a92012-01-25 23:49:49 +00001619/// getShuffleMask - Return the full mask for this instruction, where each
1620/// element is the element number and undef's are returned as -1.
Chris Lattner56243b82012-01-26 02:51:13 +00001621void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1622 SmallVectorImpl<int> &Result) {
1623 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner83694a92012-01-25 23:49:49 +00001624
Chris Lattner56243b82012-01-26 02:51:13 +00001625 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner83694a92012-01-25 23:49:49 +00001626 for (unsigned i = 0; i != NumElts; ++i)
1627 Result.push_back(CDS->getElementAsInteger(i));
1628 return;
1629 }
Chris Lattner83694a92012-01-25 23:49:49 +00001630 for (unsigned i = 0; i != NumElts; ++i) {
1631 Constant *C = Mask->getAggregateElement(i);
1632 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner220dfa72012-01-26 00:41:50 +00001633 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner83694a92012-01-25 23:49:49 +00001634 }
1635}
1636
1637
Dan Gohman041e2eb2008-05-15 19:50:34 +00001638//===----------------------------------------------------------------------===//
Dan Gohmane4569942008-05-23 00:36:11 +00001639// InsertValueInst Class
1640//===----------------------------------------------------------------------===//
1641
Jay Foadfc6d3a42011-07-13 10:26:04 +00001642void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1643 const Twine &Name) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001644 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foadfc6d3a42011-07-13 10:26:04 +00001645
1646 // There's no fundamental reason why we require at least one index
1647 // (other than weirdness with &*IdxBegin being invalid; see
1648 // getelementptr's init routine for example). But there's no
1649 // present need to support it.
1650 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1651
1652 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommela4805cf2010-12-05 20:50:26 +00001653 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001654 Op<0>() = Agg;
1655 Op<1>() = Val;
Dan Gohmane4569942008-05-23 00:36:11 +00001656
Jay Foadfc6d3a42011-07-13 10:26:04 +00001657 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001658 setName(Name);
Dan Gohmane4569942008-05-23 00:36:11 +00001659}
1660
1661InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greifb9d9eba2008-05-27 11:03:29 +00001662 : Instruction(IVI.getType(), InsertValue,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001663 OperandTraits<InsertValueInst>::op_begin(this), 2),
1664 Indices(IVI.Indices) {
Dan Gohman80b96262008-06-17 23:25:49 +00001665 Op<0>() = IVI.getOperand(0);
1666 Op<1>() = IVI.getOperand(1);
Dan Gohman58cfa3b2009-08-25 22:11:20 +00001667 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohmane4569942008-05-23 00:36:11 +00001668}
1669
1670//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001671// ExtractValueInst Class
1672//===----------------------------------------------------------------------===//
1673
Jay Foadfc6d3a42011-07-13 10:26:04 +00001674void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001675 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohmane4569942008-05-23 00:36:11 +00001676
Jay Foadfc6d3a42011-07-13 10:26:04 +00001677 // There's no fundamental reason why we require at least one index.
1678 // But there's no present need to support it.
1679 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohmane4569942008-05-23 00:36:11 +00001680
Jay Foadfc6d3a42011-07-13 10:26:04 +00001681 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001682 setName(Name);
Dan Gohmane4569942008-05-23 00:36:11 +00001683}
1684
1685ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001686 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001687 Indices(EVI.Indices) {
Dan Gohman58cfa3b2009-08-25 22:11:20 +00001688 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohmane4569942008-05-23 00:36:11 +00001689}
1690
Dan Gohman041e2eb2008-05-15 19:50:34 +00001691// getIndexedType - Returns the type of the element that would be extracted
1692// with an extractvalue instruction with the specified parameters.
1693//
1694// A null type is returned if the indices are invalid for the specified
1695// pointer type.
1696//
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001697Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001698 ArrayRef<unsigned> Idxs) {
1699 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001700 unsigned Index = Idxs[CurIdx];
Frits van Bommela4805cf2010-12-05 20:50:26 +00001701 // We can't use CompositeType::indexValid(Index) here.
1702 // indexValid() always returns true for arrays because getelementptr allows
1703 // out-of-bounds indices. Since we don't allow those for extractvalue and
1704 // insertvalue we need to check array indexing manually.
1705 // Since the only other types we can index into are struct types it's just
1706 // as easy to check those manually as well.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001707 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommela4805cf2010-12-05 20:50:26 +00001708 if (Index >= AT->getNumElements())
1709 return 0;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001710 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommela4805cf2010-12-05 20:50:26 +00001711 if (Index >= ST->getNumElements())
1712 return 0;
1713 } else {
1714 // Not a valid type to index into.
1715 return 0;
1716 }
1717
1718 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001719 }
Chris Lattner1afcace2011-07-09 17:41:24 +00001720 return const_cast<Type*>(Agg);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001721}
Chris Lattner00f10232006-04-08 01:18:18 +00001722
1723//===----------------------------------------------------------------------===//
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001724// BinaryOperator Class
1725//===----------------------------------------------------------------------===//
1726
Chris Lattner910c80a2007-02-24 00:55:48 +00001727BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001728 Type *Ty, const Twine &Name,
Chris Lattner910c80a2007-02-24 00:55:48 +00001729 Instruction *InsertBefore)
Dan Gohman1eaac532010-05-03 22:44:19 +00001730 : Instruction(Ty, iType,
Gabor Greifefe65362008-05-10 08:32:32 +00001731 OperandTraits<BinaryOperator>::op_begin(this),
1732 OperandTraits<BinaryOperator>::operands(this),
1733 InsertBefore) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001734 Op<0>() = S1;
1735 Op<1>() = S2;
Dan Gohman1eaac532010-05-03 22:44:19 +00001736 init(iType);
Chris Lattner910c80a2007-02-24 00:55:48 +00001737 setName(Name);
1738}
1739
1740BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001741 Type *Ty, const Twine &Name,
Chris Lattner910c80a2007-02-24 00:55:48 +00001742 BasicBlock *InsertAtEnd)
Dan Gohman1eaac532010-05-03 22:44:19 +00001743 : Instruction(Ty, iType,
Gabor Greifefe65362008-05-10 08:32:32 +00001744 OperandTraits<BinaryOperator>::op_begin(this),
1745 OperandTraits<BinaryOperator>::operands(this),
1746 InsertAtEnd) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001747 Op<0>() = S1;
1748 Op<1>() = S2;
Dan Gohman1eaac532010-05-03 22:44:19 +00001749 init(iType);
Chris Lattner910c80a2007-02-24 00:55:48 +00001750 setName(Name);
1751}
1752
1753
1754void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerb12261a2005-01-29 00:35:16 +00001755 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +00001756 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerb12261a2005-01-29 00:35:16 +00001757 assert(LHS->getType() == RHS->getType() &&
1758 "Binary operator operand types must match!");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001759#ifndef NDEBUG
1760 switch (iType) {
1761 case Add: case Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001762 case Mul:
Chris Lattnerb12261a2005-01-29 00:35:16 +00001763 assert(getType() == LHS->getType() &&
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001764 "Arithmetic operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001765 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001766 "Tried to create an integer operation on a non-integer type!");
1767 break;
1768 case FAdd: case FSub:
1769 case FMul:
1770 assert(getType() == LHS->getType() &&
1771 "Arithmetic operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001772 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001773 "Tried to create a floating-point operation on a "
1774 "non-floating-point type!");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001775 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001776 case UDiv:
1777 case SDiv:
1778 assert(getType() == LHS->getType() &&
1779 "Arithmetic operation should return same type as operands!");
Duncan Sands1df98592010-02-16 11:11:14 +00001780 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001781 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer1628cec2006-10-26 06:15:43 +00001782 "Incorrect operand type (not integer) for S/UDIV");
1783 break;
1784 case FDiv:
1785 assert(getType() == LHS->getType() &&
1786 "Arithmetic operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001787 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmanf57478f2009-06-15 22:25:12 +00001788 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer1628cec2006-10-26 06:15:43 +00001789 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001790 case URem:
1791 case SRem:
1792 assert(getType() == LHS->getType() &&
1793 "Arithmetic operation should return same type as operands!");
Duncan Sands1df98592010-02-16 11:11:14 +00001794 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001795 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001796 "Incorrect operand type (not integer) for S/UREM");
1797 break;
1798 case FRem:
1799 assert(getType() == LHS->getType() &&
1800 "Arithmetic operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001801 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmanf57478f2009-06-15 22:25:12 +00001802 "Incorrect operand type (not floating point) for FREM");
Reid Spencer0a783f72006-11-02 01:53:59 +00001803 break;
Reid Spencer832254e2007-02-02 02:16:23 +00001804 case Shl:
1805 case LShr:
1806 case AShr:
1807 assert(getType() == LHS->getType() &&
1808 "Shift operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001809 assert((getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00001810 (getType()->isVectorTy() &&
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001811 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begeman5bc1ea02008-07-29 15:49:41 +00001812 "Tried to create a shift operation on a non-integral type!");
Reid Spencer832254e2007-02-02 02:16:23 +00001813 break;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001814 case And: case Or:
1815 case Xor:
Chris Lattnerb12261a2005-01-29 00:35:16 +00001816 assert(getType() == LHS->getType() &&
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001817 "Logical operation should return same type as operands!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001818 assert((getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00001819 (getType()->isVectorTy() &&
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001820 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman1bae2912005-01-27 06:46:38 +00001821 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001822 break;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001823 default:
1824 break;
1825 }
1826#endif
1827}
1828
Gabor Greif7cbd8a32008-05-16 19:29:10 +00001829BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001830 const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001831 Instruction *InsertBefore) {
1832 assert(S1->getType() == S2->getType() &&
1833 "Cannot create binary operator with two operands of differing type!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001834 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001835}
1836
Gabor Greif7cbd8a32008-05-16 19:29:10 +00001837BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001838 const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001839 BasicBlock *InsertAtEnd) {
Gabor Greif7cbd8a32008-05-16 19:29:10 +00001840 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001841 InsertAtEnd->getInstList().push_back(Res);
1842 return Res;
1843}
1844
Dan Gohman4ae51262009-08-12 16:23:25 +00001845BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001846 Instruction *InsertBefore) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001847 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer24d6da52007-01-21 00:29:26 +00001848 return new BinaryOperator(Instruction::Sub,
1849 zero, Op,
1850 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001851}
1852
Dan Gohman4ae51262009-08-12 16:23:25 +00001853BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001854 BasicBlock *InsertAtEnd) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001855 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer24d6da52007-01-21 00:29:26 +00001856 return new BinaryOperator(Instruction::Sub,
1857 zero, Op,
1858 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001859}
1860
Dan Gohmanbdc46c62009-12-18 02:58:50 +00001861BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1862 Instruction *InsertBefore) {
1863 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1864 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1865}
1866
1867BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1868 BasicBlock *InsertAtEnd) {
1869 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1870 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1871}
1872
Duncan Sands8991d512010-02-02 12:53:04 +00001873BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1874 Instruction *InsertBefore) {
1875 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1876 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1877}
1878
1879BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1880 BasicBlock *InsertAtEnd) {
1881 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1882 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1883}
1884
Dan Gohman4ae51262009-08-12 16:23:25 +00001885BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001886 Instruction *InsertBefore) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001887 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner4ca829e2012-01-25 06:02:56 +00001888 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001889 Op->getType(), Name, InsertBefore);
1890}
1891
Dan Gohman4ae51262009-08-12 16:23:25 +00001892BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001893 BasicBlock *InsertAtEnd) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001894 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner4ca829e2012-01-25 06:02:56 +00001895 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001896 Op->getType(), Name, InsertAtEnd);
1897}
1898
Dan Gohman4ae51262009-08-12 16:23:25 +00001899BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001900 Instruction *InsertBefore) {
Chris Lattner4ca829e2012-01-25 06:02:56 +00001901 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnerfdbc82a2006-03-25 21:54:21 +00001902 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001903 Op->getType(), Name, InsertBefore);
1904}
1905
Dan Gohman4ae51262009-08-12 16:23:25 +00001906BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001907 BasicBlock *InsertAtEnd) {
Chris Lattner4ca829e2012-01-25 06:02:56 +00001908 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerb9d41002005-12-21 18:22:19 +00001909 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001910 Op->getType(), Name, InsertAtEnd);
1911}
1912
1913
1914// isConstantAllOnes - Helper function for several functions below
1915static inline bool isConstantAllOnes(const Value *V) {
Chris Lattnera78fa8c2012-01-27 03:08:05 +00001916 if (const Constant *C = dyn_cast<Constant>(V))
1917 return C->isAllOnesValue();
Chris Lattnere20b7be2007-06-15 06:04:24 +00001918 return false;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001919}
1920
Owen Andersonfa82b6e2009-07-13 22:18:28 +00001921bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001922 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1923 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonfa82b6e2009-07-13 22:18:28 +00001924 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1925 return C->isNegativeZeroValue();
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001926 return false;
1927}
1928
Shuxin Yang935e35d2013-01-09 00:13:41 +00001929bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001930 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1931 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yang935e35d2013-01-09 00:13:41 +00001932 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
1933 if (!IgnoreZeroSign)
1934 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
1935 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
1936 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001937 return false;
1938}
1939
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001940bool BinaryOperator::isNot(const Value *V) {
1941 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1942 return (Bop->getOpcode() == Instruction::Xor &&
1943 (isConstantAllOnes(Bop->getOperand(1)) ||
1944 isConstantAllOnes(Bop->getOperand(0))));
1945 return false;
1946}
1947
Chris Lattner64001d02005-04-24 07:28:37 +00001948Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner64001d02005-04-24 07:28:37 +00001949 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001950}
1951
Chris Lattner64001d02005-04-24 07:28:37 +00001952const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1953 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001954}
1955
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001956Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001957 return cast<BinaryOperator>(BinOp)->getOperand(1);
1958}
1959
1960const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1961 return getFNegArgument(const_cast<Value*>(BinOp));
1962}
1963
Chris Lattner64001d02005-04-24 07:28:37 +00001964Value *BinaryOperator::getNotArgument(Value *BinOp) {
1965 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1966 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1967 Value *Op0 = BO->getOperand(0);
1968 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001969 if (isConstantAllOnes(Op0)) return Op1;
1970
1971 assert(isConstantAllOnes(Op1));
1972 return Op0;
1973}
1974
Chris Lattner64001d02005-04-24 07:28:37 +00001975const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1976 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001977}
1978
1979
1980// swapOperands - Exchange the two operands to this instruction. This
1981// instruction is safe to use on any binary instruction and does not
1982// modify the semantics of the instruction. If the instruction is
1983// order dependent (SetLT f.e.) the opcode is changed.
1984//
1985bool BinaryOperator::swapOperands() {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001986 if (!isCommutative())
1987 return true; // Can't commute operands
Gabor Greif94fb68b2008-05-13 22:51:52 +00001988 Op<0>().swap(Op<1>());
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00001989 return false;
1990}
1991
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001992void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1993 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1994}
1995
1996void BinaryOperator::setHasNoSignedWrap(bool b) {
1997 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1998}
1999
2000void BinaryOperator::setIsExact(bool b) {
Chris Lattner35bda892011-02-06 21:44:57 +00002001 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002002}
2003
Nick Lewyckyae05e7d2009-09-27 21:33:04 +00002004bool BinaryOperator::hasNoUnsignedWrap() const {
2005 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2006}
2007
2008bool BinaryOperator::hasNoSignedWrap() const {
2009 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2010}
2011
2012bool BinaryOperator::isExact() const {
Chris Lattner35bda892011-02-06 21:44:57 +00002013 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewyckyae05e7d2009-09-27 21:33:04 +00002014}
2015
Chris Lattner79bc3322006-09-18 04:54:57 +00002016//===----------------------------------------------------------------------===//
Duncan Sands8883c432012-04-16 16:28:59 +00002017// FPMathOperator Class
2018//===----------------------------------------------------------------------===//
2019
2020/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2021/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands2867c852012-04-16 19:39:33 +00002022/// default precision.
Duncan Sands8883c432012-04-16 16:28:59 +00002023float FPMathOperator::getFPAccuracy() const {
2024 const MDNode *MD =
2025 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2026 if (!MD)
2027 return 0.0;
Duncan Sands2867c852012-04-16 19:39:33 +00002028 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2029 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands8883c432012-04-16 16:28:59 +00002030}
2031
2032
2033//===----------------------------------------------------------------------===//
Chris Lattner79bc3322006-09-18 04:54:57 +00002034// CastInst Class
2035//===----------------------------------------------------------------------===//
2036
David Blaikie0becc962011-12-01 08:00:17 +00002037void CastInst::anchor() {}
2038
Reid Spencer3da59db2006-11-27 01:05:10 +00002039// Just determine if this cast only deals with integral->integral conversion.
2040bool CastInst::isIntegerCast() const {
2041 switch (getOpcode()) {
2042 default: return false;
2043 case Instruction::ZExt:
2044 case Instruction::SExt:
2045 case Instruction::Trunc:
2046 return true;
2047 case Instruction::BitCast:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002048 return getOperand(0)->getType()->isIntegerTy() &&
2049 getType()->isIntegerTy();
Reid Spencer3da59db2006-11-27 01:05:10 +00002050 }
Chris Lattner79bc3322006-09-18 04:54:57 +00002051}
2052
Reid Spencer3da59db2006-11-27 01:05:10 +00002053bool CastInst::isLosslessCast() const {
2054 // Only BitCast can be lossless, exit fast if we're not BitCast
2055 if (getOpcode() != Instruction::BitCast)
2056 return false;
2057
2058 // Identity cast is always lossless
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002059 Type* SrcTy = getOperand(0)->getType();
2060 Type* DstTy = getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00002061 if (SrcTy == DstTy)
2062 return true;
2063
Reid Spencer79e21d32006-12-31 05:26:44 +00002064 // Pointer to pointer is always lossless.
Duncan Sands1df98592010-02-16 11:11:14 +00002065 if (SrcTy->isPointerTy())
2066 return DstTy->isPointerTy();
Reid Spencer3da59db2006-11-27 01:05:10 +00002067 return false; // Other types have no identity values
2068}
2069
2070/// This function determines if the CastInst does not require any bits to be
2071/// changed in order to effect the cast. Essentially, it identifies cases where
2072/// no code gen is necessary for the cast, hence the name no-op cast. For
2073/// example, the following are all no-op casts:
Dan Gohman9b38d7d2008-05-12 16:34:30 +00002074/// # bitcast i32* %x to i8*
2075/// # bitcast <2 x i32> %x to <4 x i16>
2076/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman2c048ea2010-05-28 21:41:37 +00002077/// @brief Determine if the described cast is a no-op.
2078bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002079 Type *SrcTy,
2080 Type *DestTy,
2081 Type *IntPtrTy) {
Dan Gohman2c048ea2010-05-28 21:41:37 +00002082 switch (Opcode) {
Craig Topper50bee422012-02-05 22:14:15 +00002083 default: llvm_unreachable("Invalid CastOp");
Reid Spencer3da59db2006-11-27 01:05:10 +00002084 case Instruction::Trunc:
2085 case Instruction::ZExt:
2086 case Instruction::SExt:
2087 case Instruction::FPTrunc:
2088 case Instruction::FPExt:
2089 case Instruction::UIToFP:
2090 case Instruction::SIToFP:
2091 case Instruction::FPToUI:
2092 case Instruction::FPToSI:
2093 return false; // These always modify bits
2094 case Instruction::BitCast:
2095 return true; // BitCast never modifies bits.
2096 case Instruction::PtrToInt:
Dan Gohman6de29f82009-06-15 22:12:54 +00002097 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman2c048ea2010-05-28 21:41:37 +00002098 DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002099 case Instruction::IntToPtr:
Dan Gohman6de29f82009-06-15 22:12:54 +00002100 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman2c048ea2010-05-28 21:41:37 +00002101 SrcTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002102 }
2103}
2104
Dan Gohman2c048ea2010-05-28 21:41:37 +00002105/// @brief Determine if a cast is a no-op.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002106bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman2c048ea2010-05-28 21:41:37 +00002107 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2108}
2109
Reid Spencer3da59db2006-11-27 01:05:10 +00002110/// This function determines if a pair of casts can be eliminated and what
2111/// opcode should be used in the elimination. This assumes that there are two
2112/// instructions like this:
2113/// * %F = firstOpcode SrcTy %x to MidTy
2114/// * %S = secondOpcode MidTy %F to DstTy
2115/// The function returns a resultOpcode so these two casts can be replaced with:
2116/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2117/// If no such cast is permited, the function returns 0.
2118unsigned CastInst::isEliminableCastPair(
2119 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sands446cf942012-10-30 16:03:32 +00002120 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2121 Type *DstIntPtrTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00002122 // Define the 144 possibilities for these two cast instructions. The values
2123 // in this matrix determine what to do in a given situation and select the
2124 // case in the switch below. The rows correspond to firstOp, the columns
2125 // correspond to secondOp. In looking at the table below, keep in mind
2126 // the following cast properties:
2127 //
2128 // Size Compare Source Destination
2129 // Operator Src ? Size Type Sign Type Sign
2130 // -------- ------------ ------------------- ---------------------
2131 // TRUNC > Integer Any Integral Any
2132 // ZEXT < Integral Unsigned Integer Any
2133 // SEXT < Integral Signed Integer Any
2134 // FPTOUI n/a FloatPt n/a Integral Unsigned
2135 // FPTOSI n/a FloatPt n/a Integral Signed
2136 // UITOFP n/a Integral Unsigned FloatPt n/a
2137 // SITOFP n/a Integral Signed FloatPt n/a
2138 // FPTRUNC > FloatPt n/a FloatPt n/a
2139 // FPEXT < FloatPt n/a FloatPt n/a
2140 // PTRTOINT n/a Pointer n/a Integral Unsigned
2141 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmana5ced592010-04-07 23:22:42 +00002142 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner518f6fa2006-12-05 23:43:59 +00002143 //
2144 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohmana119de82009-06-14 23:30:43 +00002145 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2146 // into "fptoui double to i64", but this loses information about the range
Chris Lattner518f6fa2006-12-05 23:43:59 +00002147 // of the produced value (we no longer know the top-part is all zeros).
2148 // Further this conversion is often much more expensive for typical hardware,
2149 // and causes issues when building libgcc. We disallow fptosi+sext for the
2150 // same reason.
Reid Spencer3da59db2006-11-27 01:05:10 +00002151 const unsigned numCastOps =
2152 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2153 static const uint8_t CastResults[numCastOps][numCastOps] = {
2154 // T F F U S F F P I B -+
2155 // R Z S P P I I T P 2 N T |
2156 // U E E 2 2 2 2 R E I T C +- secondOp
2157 // N X X U S F F N X N 2 V |
2158 // C T T I I P P C T T P T -+
2159 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2160 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2161 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner518f6fa2006-12-05 23:43:59 +00002162 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2163 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer3da59db2006-11-27 01:05:10 +00002164 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2165 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2166 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2167 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2168 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2169 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2170 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2171 };
Chris Lattnerdfd36262010-07-12 01:19:22 +00002172
2173 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem89879ec2011-08-29 19:58:36 +00002174 // merging. However, bitcast of A->B->A are allowed.
2175 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2176 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2177 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2178
2179 // Check if any of the bitcasts convert scalars<->vectors.
2180 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2181 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2182 // Unless we are bitcasing to the original type, disallow optimizations.
2183 if (!chainedBitcast) return 0;
Reid Spencer3da59db2006-11-27 01:05:10 +00002184
2185 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2186 [secondOp-Instruction::CastOpsBegin];
2187 switch (ElimCase) {
2188 case 0:
2189 // categorically disallowed
2190 return 0;
2191 case 1:
2192 // allowed, use first cast's opcode
2193 return firstOp;
2194 case 2:
2195 // allowed, use second cast's opcode
2196 return secondOp;
2197 case 3:
2198 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange4a0a152010-01-23 04:35:57 +00002199 // is integer and we are not converting between a vector and a
Chris Lattner33b17582010-01-23 04:42:42 +00002200 // non vector type.
Duncan Sands1df98592010-02-16 11:11:14 +00002201 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002202 return firstOp;
2203 return 0;
2204 case 4:
2205 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner33b17582010-01-23 04:42:42 +00002206 // is floating point.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002207 if (DstTy->isFloatingPointTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002208 return firstOp;
2209 return 0;
2210 case 5:
2211 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner33b17582010-01-23 04:42:42 +00002212 // is an integer.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002213 if (SrcTy->isIntegerTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002214 return secondOp;
2215 return 0;
2216 case 6:
2217 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner33b17582010-01-23 04:42:42 +00002218 // is a floating point.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002219 if (SrcTy->isFloatingPointTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002220 return secondOp;
2221 return 0;
2222 case 7: {
2223 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Duncan Sands446cf942012-10-30 16:03:32 +00002224 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman295643b2009-07-21 23:19:40 +00002225 return 0;
Duncan Sands446cf942012-10-30 16:03:32 +00002226 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Dan Gohman6de29f82009-06-15 22:12:54 +00002227 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002228 if (MidSize >= PtrSize)
2229 return Instruction::BitCast;
2230 return 0;
2231 }
2232 case 8: {
2233 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2234 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2235 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman6de29f82009-06-15 22:12:54 +00002236 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2237 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002238 if (SrcSize == DstSize)
2239 return Instruction::BitCast;
2240 else if (SrcSize < DstSize)
2241 return firstOp;
2242 return secondOp;
2243 }
2244 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2245 return Instruction::ZExt;
2246 case 10:
2247 // fpext followed by ftrunc is allowed if the bit size returned to is
2248 // the same as the original, in which case its just a bitcast
2249 if (SrcTy == DstTy)
2250 return Instruction::BitCast;
2251 return 0; // If the types are not the same we can't eliminate it.
2252 case 11:
2253 // bitcast followed by ptrtoint is allowed as long as the bitcast
2254 // is a pointer to pointer cast.
Duncan Sands1df98592010-02-16 11:11:14 +00002255 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002256 return secondOp;
2257 return 0;
2258 case 12:
2259 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands1df98592010-02-16 11:11:14 +00002260 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002261 return firstOp;
2262 return 0;
2263 case 13: {
2264 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sands446cf942012-10-30 16:03:32 +00002265 if (!MidIntPtrTy)
Dan Gohman295643b2009-07-21 23:19:40 +00002266 return 0;
Duncan Sands446cf942012-10-30 16:03:32 +00002267 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman6de29f82009-06-15 22:12:54 +00002268 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2269 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002270 if (SrcSize <= PtrSize && SrcSize == DstSize)
2271 return Instruction::BitCast;
2272 return 0;
2273 }
2274 case 99:
2275 // cast combination can't happen (error in input). This is for all cases
2276 // where the MidTy is not the same for the two cast instructions.
Craig Topper50bee422012-02-05 22:14:15 +00002277 llvm_unreachable("Invalid Cast Combination");
Reid Spencer3da59db2006-11-27 01:05:10 +00002278 default:
Craig Topper50bee422012-02-05 22:14:15 +00002279 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002280 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002281}
2282
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002283CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002284 const Twine &Name, Instruction *InsertBefore) {
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002285 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002286 // Construct and return the appropriate CastInst subclass
2287 switch (op) {
2288 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2289 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2290 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2291 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2292 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2293 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2294 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2295 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2296 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2297 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2298 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2299 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topper50bee422012-02-05 22:14:15 +00002300 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer3da59db2006-11-27 01:05:10 +00002301 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002302}
2303
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002304CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002305 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002306 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002307 // Construct and return the appropriate CastInst subclass
2308 switch (op) {
2309 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2310 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2311 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2312 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2313 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2314 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2315 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2316 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2317 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2318 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2319 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2320 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topper50bee422012-02-05 22:14:15 +00002321 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer3da59db2006-11-27 01:05:10 +00002322 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002323}
2324
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002325CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002326 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002327 Instruction *InsertBefore) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002328 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002329 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2330 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer848414e2006-12-04 20:17:56 +00002331}
2332
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002333CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002334 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002335 BasicBlock *InsertAtEnd) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002336 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002337 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2338 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer848414e2006-12-04 20:17:56 +00002339}
2340
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002341CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002342 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002343 Instruction *InsertBefore) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002344 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002345 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2346 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer848414e2006-12-04 20:17:56 +00002347}
2348
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002349CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002350 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002351 BasicBlock *InsertAtEnd) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002352 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002353 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2354 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer848414e2006-12-04 20:17:56 +00002355}
2356
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002357CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002358 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002359 Instruction *InsertBefore) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002360 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002361 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2362 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer848414e2006-12-04 20:17:56 +00002363}
2364
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002365CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002366 const Twine &Name,
Reid Spencer848414e2006-12-04 20:17:56 +00002367 BasicBlock *InsertAtEnd) {
Dan Gohman6de29f82009-06-15 22:12:54 +00002368 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002369 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2370 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer848414e2006-12-04 20:17:56 +00002371}
2372
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002373CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002374 const Twine &Name,
Reid Spencer330d86d2006-12-05 03:28:26 +00002375 BasicBlock *InsertAtEnd) {
Duncan Sands1df98592010-02-16 11:11:14 +00002376 assert(S->getType()->isPointerTy() && "Invalid cast");
2377 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencer330d86d2006-12-05 03:28:26 +00002378 "Invalid cast");
2379
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002380 if (Ty->isIntegerTy())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002381 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2382 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencer330d86d2006-12-05 03:28:26 +00002383}
2384
2385/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002386CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002387 const Twine &Name,
Reid Spencer330d86d2006-12-05 03:28:26 +00002388 Instruction *InsertBefore) {
Duncan Sands1df98592010-02-16 11:11:14 +00002389 assert(S->getType()->isPointerTy() && "Invalid cast");
2390 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencer330d86d2006-12-05 03:28:26 +00002391 "Invalid cast");
2392
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002393 if (Ty->isIntegerTy())
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002394 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2395 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencer330d86d2006-12-05 03:28:26 +00002396}
2397
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002398CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002399 bool isSigned, const Twine &Name,
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002400 Instruction *InsertBefore) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002401 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner859c3722010-01-10 20:21:42 +00002402 "Invalid integer cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00002403 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2404 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002405 Instruction::CastOps opcode =
2406 (SrcBits == DstBits ? Instruction::BitCast :
2407 (SrcBits > DstBits ? Instruction::Trunc :
2408 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002409 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002410}
2411
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002412CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002413 bool isSigned, const Twine &Name,
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002414 BasicBlock *InsertAtEnd) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002415 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohmanf57478f2009-06-15 22:25:12 +00002416 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00002417 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2418 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002419 Instruction::CastOps opcode =
2420 (SrcBits == DstBits ? Instruction::BitCast :
2421 (SrcBits > DstBits ? Instruction::Trunc :
2422 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002423 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002424}
2425
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002426CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002427 const Twine &Name,
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002428 Instruction *InsertBefore) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002429 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002430 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00002431 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2432 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002433 Instruction::CastOps opcode =
2434 (SrcBits == DstBits ? Instruction::BitCast :
2435 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002436 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002437}
2438
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002439CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002440 const Twine &Name,
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002441 BasicBlock *InsertAtEnd) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002442 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002443 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00002444 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2445 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002446 Instruction::CastOps opcode =
2447 (SrcBits == DstBits ? Instruction::BitCast :
2448 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002449 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer6d81a7d2006-12-12 00:49:44 +00002450}
2451
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002452// Check whether it is valid to call getCastOpcode for these types.
2453// This routine must be kept in sync with getCastOpcode.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002454bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002455 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2456 return false;
2457
2458 if (SrcTy == DestTy)
2459 return true;
2460
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002461 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2462 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sands117feba2011-05-18 07:13:41 +00002463 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2464 // An element by element cast. Valid if casting the elements is valid.
2465 SrcTy = SrcVecTy->getElementType();
2466 DestTy = DestVecTy->getElementType();
2467 }
2468
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002469 // Get the bit sizes, we'll need these
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002470 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2471 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002472
2473 // Run through the possibilities ...
Duncan Sands7016ec12011-05-18 10:59:25 +00002474 if (DestTy->isIntegerTy()) { // Casting to integral
2475 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002476 return true;
Duncan Sands7016ec12011-05-18 10:59:25 +00002477 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002478 return true;
Duncan Sands7016ec12011-05-18 10:59:25 +00002479 } else if (SrcTy->isVectorTy()) { // Casting from vector
2480 return DestBits == SrcBits;
Gabor Greifb1dbcd82008-05-15 10:04:30 +00002481 } else { // Casting from something else
Duncan Sands1df98592010-02-16 11:11:14 +00002482 return SrcTy->isPointerTy();
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002483 }
Duncan Sands7016ec12011-05-18 10:59:25 +00002484 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2485 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002486 return true;
Duncan Sands7016ec12011-05-18 10:59:25 +00002487 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002488 return true;
Duncan Sands7016ec12011-05-18 10:59:25 +00002489 } else if (SrcTy->isVectorTy()) { // Casting from vector
2490 return DestBits == SrcBits;
Gabor Greifb1dbcd82008-05-15 10:04:30 +00002491 } else { // Casting from something else
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002492 return false;
2493 }
Duncan Sands7016ec12011-05-18 10:59:25 +00002494 } else if (DestTy->isVectorTy()) { // Casting to vector
2495 return DestBits == SrcBits;
Duncan Sands1df98592010-02-16 11:11:14 +00002496 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands7016ec12011-05-18 10:59:25 +00002497 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002498 return true;
Duncan Sands7016ec12011-05-18 10:59:25 +00002499 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002500 return true;
Duncan Sands7016ec12011-05-18 10:59:25 +00002501 } else { // Casting from something else
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002502 return false;
2503 }
Duncan Sands60794652011-04-01 03:34:54 +00002504 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands7016ec12011-05-18 10:59:25 +00002505 if (SrcTy->isVectorTy()) {
2506 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands60794652011-04-01 03:34:54 +00002507 } else {
2508 return false;
2509 }
Duncan Sands7016ec12011-05-18 10:59:25 +00002510 } else { // Casting to something else
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002511 return false;
2512 }
2513}
2514
Reid Spencer3da59db2006-11-27 01:05:10 +00002515// Provide a way to get a "cast" where the cast opcode is inferred from the
2516// types and size of the operand. This, basically, is a parallel of the
Reid Spencer0a11af12007-01-17 02:46:11 +00002517// logic in the castIsValid function below. This axiom should hold:
2518// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2519// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer3da59db2006-11-27 01:05:10 +00002520// casting opcode for the arguments passed to it.
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002521// This routine must be kept in sync with isCastable.
Reid Spencer3da59db2006-11-27 01:05:10 +00002522Instruction::CastOps
Reid Spencer56667122006-12-04 02:43:42 +00002523CastInst::getCastOpcode(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002524 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2525 Type *SrcTy = Src->getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00002526
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002527 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2528 "Only first class types are castable!");
2529
Duncan Sands117feba2011-05-18 07:13:41 +00002530 if (SrcTy == DestTy)
2531 return BitCast;
2532
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002533 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2534 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sands117feba2011-05-18 07:13:41 +00002535 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2536 // An element by element cast. Find the appropriate opcode based on the
2537 // element types.
2538 SrcTy = SrcVecTy->getElementType();
2539 DestTy = DestVecTy->getElementType();
2540 }
2541
2542 // Get the bit sizes, we'll need these
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002543 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2544 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands117feba2011-05-18 07:13:41 +00002545
Reid Spencer3da59db2006-11-27 01:05:10 +00002546 // Run through the possibilities ...
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002547 if (DestTy->isIntegerTy()) { // Casting to integral
2548 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer3da59db2006-11-27 01:05:10 +00002549 if (DestBits < SrcBits)
2550 return Trunc; // int -> smaller int
2551 else if (DestBits > SrcBits) { // its an extension
Reid Spencer56667122006-12-04 02:43:42 +00002552 if (SrcIsSigned)
Reid Spencer3da59db2006-11-27 01:05:10 +00002553 return SExt; // signed -> SEXT
2554 else
2555 return ZExt; // unsigned -> ZEXT
2556 } else {
2557 return BitCast; // Same size, No-op cast
2558 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002559 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer56667122006-12-04 02:43:42 +00002560 if (DestIsSigned)
Reid Spencer3da59db2006-11-27 01:05:10 +00002561 return FPToSI; // FP -> sint
2562 else
2563 return FPToUI; // FP -> uint
Duncan Sands7016ec12011-05-18 10:59:25 +00002564 } else if (SrcTy->isVectorTy()) {
2565 assert(DestBits == SrcBits &&
2566 "Casting vector to integer of different width");
Reid Spencer3da59db2006-11-27 01:05:10 +00002567 return BitCast; // Same size, no-op cast
2568 } else {
Duncan Sands1df98592010-02-16 11:11:14 +00002569 assert(SrcTy->isPointerTy() &&
Reid Spencer3da59db2006-11-27 01:05:10 +00002570 "Casting from a value that is not first-class type");
2571 return PtrToInt; // ptr -> int
2572 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002573 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2574 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer56667122006-12-04 02:43:42 +00002575 if (SrcIsSigned)
Reid Spencer3da59db2006-11-27 01:05:10 +00002576 return SIToFP; // sint -> FP
2577 else
2578 return UIToFP; // uint -> FP
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002579 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer3da59db2006-11-27 01:05:10 +00002580 if (DestBits < SrcBits) {
2581 return FPTrunc; // FP -> smaller FP
2582 } else if (DestBits > SrcBits) {
2583 return FPExt; // FP -> larger FP
2584 } else {
2585 return BitCast; // same size, no-op cast
2586 }
Duncan Sands7016ec12011-05-18 10:59:25 +00002587 } else if (SrcTy->isVectorTy()) {
2588 assert(DestBits == SrcBits &&
Dan Gohman86296cc2007-05-11 21:43:24 +00002589 "Casting vector to floating point of different width");
Devang Patel8c3b47f2008-11-05 01:37:40 +00002590 return BitCast; // same size, no-op cast
Reid Spencer3da59db2006-11-27 01:05:10 +00002591 }
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00002592 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands7016ec12011-05-18 10:59:25 +00002593 } else if (DestTy->isVectorTy()) {
2594 assert(DestBits == SrcBits &&
2595 "Illegal cast to vector (wrong type or size)");
2596 return BitCast;
Duncan Sands1df98592010-02-16 11:11:14 +00002597 } else if (DestTy->isPointerTy()) {
2598 if (SrcTy->isPointerTy()) {
Reid Spencer3da59db2006-11-27 01:05:10 +00002599 return BitCast; // ptr -> ptr
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002600 } else if (SrcTy->isIntegerTy()) {
Reid Spencer3da59db2006-11-27 01:05:10 +00002601 return IntToPtr; // int -> ptr
Reid Spencer3da59db2006-11-27 01:05:10 +00002602 }
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00002603 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesen0488fb62010-09-30 23:57:10 +00002604 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands7016ec12011-05-18 10:59:25 +00002605 if (SrcTy->isVectorTy()) {
2606 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesen0488fb62010-09-30 23:57:10 +00002607 return BitCast; // 64-bit vector to MMX
Dale Johannesen0488fb62010-09-30 23:57:10 +00002608 }
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00002609 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer3da59db2006-11-27 01:05:10 +00002610 }
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00002611 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer3da59db2006-11-27 01:05:10 +00002612}
2613
2614//===----------------------------------------------------------------------===//
2615// CastInst SubClass Constructors
2616//===----------------------------------------------------------------------===//
2617
2618/// Check that the construction parameters for a CastInst are correct. This
2619/// could be broken out into the separate constructors but it is useful to have
2620/// it in one place and to eliminate the redundant code for getting the sizes
2621/// of the types involved.
Reid Spencer0a11af12007-01-17 02:46:11 +00002622bool
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002623CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00002624
2625 // Check for type sanity on the arguments
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002626 Type *SrcTy = S->getType();
Chris Lattner0b68a002010-01-26 21:51:43 +00002627 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2628 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer3da59db2006-11-27 01:05:10 +00002629 return false;
2630
2631 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00002632 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2633 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002634
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002635 // If these are vector types, get the lengths of the vectors (using zero for
2636 // scalar types means that checking that vector lengths match also checks that
2637 // scalars are not being converted to vectors or vectors to scalars).
2638 unsigned SrcLength = SrcTy->isVectorTy() ?
2639 cast<VectorType>(SrcTy)->getNumElements() : 0;
2640 unsigned DstLength = DstTy->isVectorTy() ?
2641 cast<VectorType>(DstTy)->getNumElements() : 0;
2642
Reid Spencer3da59db2006-11-27 01:05:10 +00002643 // Switch on the opcode provided
2644 switch (op) {
2645 default: return false; // This is an input error
2646 case Instruction::Trunc:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002647 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2648 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer3da59db2006-11-27 01:05:10 +00002649 case Instruction::ZExt:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002650 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2651 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer3da59db2006-11-27 01:05:10 +00002652 case Instruction::SExt:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002653 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2654 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer3da59db2006-11-27 01:05:10 +00002655 case Instruction::FPTrunc:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002656 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2657 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer3da59db2006-11-27 01:05:10 +00002658 case Instruction::FPExt:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002659 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2660 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer3da59db2006-11-27 01:05:10 +00002661 case Instruction::UIToFP:
Reid Spencer3da59db2006-11-27 01:05:10 +00002662 case Instruction::SIToFP:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002663 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2664 SrcLength == DstLength;
Reid Spencer3da59db2006-11-27 01:05:10 +00002665 case Instruction::FPToUI:
Reid Spencer3da59db2006-11-27 01:05:10 +00002666 case Instruction::FPToSI:
Duncan Sandsbb1695e2011-05-18 09:21:57 +00002667 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2668 SrcLength == DstLength;
Reid Spencer3da59db2006-11-27 01:05:10 +00002669 case Instruction::PtrToInt:
Chris Lattneraf7b4fb2012-01-25 01:32:59 +00002670 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem16087692011-12-05 06:29:09 +00002671 return false;
Chris Lattneraf7b4fb2012-01-25 01:32:59 +00002672 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2673 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2674 return false;
Nadav Rotem16087692011-12-05 06:29:09 +00002675 return SrcTy->getScalarType()->isPointerTy() &&
2676 DstTy->getScalarType()->isIntegerTy();
Reid Spencer3da59db2006-11-27 01:05:10 +00002677 case Instruction::IntToPtr:
Chris Lattneraf7b4fb2012-01-25 01:32:59 +00002678 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem16087692011-12-05 06:29:09 +00002679 return false;
Chris Lattneraf7b4fb2012-01-25 01:32:59 +00002680 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2681 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2682 return false;
Nadav Rotem16087692011-12-05 06:29:09 +00002683 return SrcTy->getScalarType()->isIntegerTy() &&
2684 DstTy->getScalarType()->isPointerTy();
Reid Spencer3da59db2006-11-27 01:05:10 +00002685 case Instruction::BitCast:
2686 // BitCast implies a no-op cast of type only. No bits change.
2687 // However, you can't cast pointers to anything but pointers.
Duncan Sands1df98592010-02-16 11:11:14 +00002688 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer3da59db2006-11-27 01:05:10 +00002689 return false;
2690
Duncan Sandsa9d0c9d2008-01-06 10:12:28 +00002691 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer3da59db2006-11-27 01:05:10 +00002692 // these cases, the cast is okay if the source and destination bit widths
2693 // are identical.
Dan Gohman6de29f82009-06-15 22:12:54 +00002694 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00002695 }
2696}
2697
2698TruncInst::TruncInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002699 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002700) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002701 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer3da59db2006-11-27 01:05:10 +00002702}
2703
2704TruncInst::TruncInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002705 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002706) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002707 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer3da59db2006-11-27 01:05:10 +00002708}
2709
2710ZExtInst::ZExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002711 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002712) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002713 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00002714}
2715
2716ZExtInst::ZExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002717 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002718) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002719 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00002720}
2721SExtInst::SExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002722 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002723) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002724 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00002725}
2726
Jeff Cohen97af7512006-12-02 02:22:01 +00002727SExtInst::SExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002728 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002729) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002730 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00002731}
2732
2733FPTruncInst::FPTruncInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002734 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002735) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002736 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer3da59db2006-11-27 01:05:10 +00002737}
2738
2739FPTruncInst::FPTruncInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002740 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002741) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002742 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer3da59db2006-11-27 01:05:10 +00002743}
2744
2745FPExtInst::FPExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002746 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002747) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002748 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00002749}
2750
2751FPExtInst::FPExtInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002752 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002753) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002754 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer3da59db2006-11-27 01:05:10 +00002755}
2756
2757UIToFPInst::UIToFPInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002758 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002759) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002760 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer3da59db2006-11-27 01:05:10 +00002761}
2762
2763UIToFPInst::UIToFPInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002764 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002765) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002766 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer3da59db2006-11-27 01:05:10 +00002767}
2768
2769SIToFPInst::SIToFPInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002770 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002771) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002772 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer3da59db2006-11-27 01:05:10 +00002773}
2774
2775SIToFPInst::SIToFPInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002776 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002777) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002778 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer3da59db2006-11-27 01:05:10 +00002779}
2780
2781FPToUIInst::FPToUIInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002782 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002783) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002784 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer3da59db2006-11-27 01:05:10 +00002785}
2786
2787FPToUIInst::FPToUIInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002788 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002789) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002790 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer3da59db2006-11-27 01:05:10 +00002791}
2792
2793FPToSIInst::FPToSIInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002794 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002795) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002796 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer3da59db2006-11-27 01:05:10 +00002797}
2798
2799FPToSIInst::FPToSIInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002800 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002801) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002802 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer3da59db2006-11-27 01:05:10 +00002803}
2804
2805PtrToIntInst::PtrToIntInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002806 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002807) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002808 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer3da59db2006-11-27 01:05:10 +00002809}
2810
2811PtrToIntInst::PtrToIntInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002812 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002813) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002814 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer3da59db2006-11-27 01:05:10 +00002815}
2816
2817IntToPtrInst::IntToPtrInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002818 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002819) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002820 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer3da59db2006-11-27 01:05:10 +00002821}
2822
2823IntToPtrInst::IntToPtrInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002824 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002825) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002826 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer3da59db2006-11-27 01:05:10 +00002827}
2828
2829BitCastInst::BitCastInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002830 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer3da59db2006-11-27 01:05:10 +00002831) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002832 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer3da59db2006-11-27 01:05:10 +00002833}
2834
2835BitCastInst::BitCastInst(
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002836 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer3da59db2006-11-27 01:05:10 +00002837) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer0a11af12007-01-17 02:46:11 +00002838 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer3da59db2006-11-27 01:05:10 +00002839}
Chris Lattner2f463862006-09-17 19:29:56 +00002840
2841//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +00002842// CmpInst Classes
2843//===----------------------------------------------------------------------===//
2844
Craig Toppera96a1822012-09-23 02:12:10 +00002845void CmpInst::anchor() {}
Chris Lattner36fe1982010-01-22 06:25:37 +00002846
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002847CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002848 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemanac80ade2008-05-12 19:01:56 +00002849 Instruction *InsertBefore)
Nate Begemanc83ad0d2008-05-12 20:11:05 +00002850 : Instruction(ty, op,
Gabor Greifefe65362008-05-10 08:32:32 +00002851 OperandTraits<CmpInst>::op_begin(this),
2852 OperandTraits<CmpInst>::operands(this),
2853 InsertBefore) {
Gabor Greif6c80c382008-05-26 21:33:52 +00002854 Op<0>() = LHS;
2855 Op<1>() = RHS;
Chris Lattnercafe9bb2009-12-29 02:14:09 +00002856 setPredicate((Predicate)predicate);
Reid Spencer97c0e212007-04-11 13:04:48 +00002857 setName(Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00002858}
Gabor Greifefe65362008-05-10 08:32:32 +00002859
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002860CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002861 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemanac80ade2008-05-12 19:01:56 +00002862 BasicBlock *InsertAtEnd)
Nate Begemanc83ad0d2008-05-12 20:11:05 +00002863 : Instruction(ty, op,
Gabor Greifefe65362008-05-10 08:32:32 +00002864 OperandTraits<CmpInst>::op_begin(this),
2865 OperandTraits<CmpInst>::operands(this),
2866 InsertAtEnd) {
Gabor Greif6c80c382008-05-26 21:33:52 +00002867 Op<0>() = LHS;
2868 Op<1>() = RHS;
Chris Lattnercafe9bb2009-12-29 02:14:09 +00002869 setPredicate((Predicate)predicate);
Reid Spencer97c0e212007-04-11 13:04:48 +00002870 setName(Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00002871}
2872
2873CmpInst *
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002874CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson333c4002009-07-09 23:48:35 +00002875 Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002876 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00002877 if (Op == Instruction::ICmp) {
Owen Anderson333c4002009-07-09 23:48:35 +00002878 if (InsertBefore)
2879 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2880 S1, S2, Name);
2881 else
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002882 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson333c4002009-07-09 23:48:35 +00002883 S1, S2, Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00002884 }
Owen Anderson333c4002009-07-09 23:48:35 +00002885
2886 if (InsertBefore)
2887 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2888 S1, S2, Name);
2889 else
Dan Gohman1c8a23c2009-08-25 23:17:54 +00002890 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson333c4002009-07-09 23:48:35 +00002891 S1, S2, Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00002892}
2893
2894CmpInst *
Gabor Greif7cbd8a32008-05-16 19:29:10 +00002895CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002896 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00002897 if (Op == Instruction::ICmp) {
Owen Anderson333c4002009-07-09 23:48:35 +00002898 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2899 S1, S2, Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00002900 }
Owen Anderson333c4002009-07-09 23:48:35 +00002901 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2902 S1, S2, Name);
Reid Spencer45fb3f32006-11-20 01:22:35 +00002903}
2904
2905void CmpInst::swapOperands() {
2906 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2907 IC->swapOperands();
2908 else
2909 cast<FCmpInst>(this)->swapOperands();
2910}
2911
Duncan Sandsc449a222011-01-04 12:52:29 +00002912bool CmpInst::isCommutative() const {
2913 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencer45fb3f32006-11-20 01:22:35 +00002914 return IC->isCommutative();
2915 return cast<FCmpInst>(this)->isCommutative();
2916}
2917
Duncan Sandsc449a222011-01-04 12:52:29 +00002918bool CmpInst::isEquality() const {
2919 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencer45fb3f32006-11-20 01:22:35 +00002920 return IC->isEquality();
2921 return cast<FCmpInst>(this)->isEquality();
2922}
2923
2924
Dan Gohman7e2dd662008-05-31 02:47:54 +00002925CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00002926 switch (pred) {
Craig Topper50bee422012-02-05 22:14:15 +00002927 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencer45fb3f32006-11-20 01:22:35 +00002928 case ICMP_EQ: return ICMP_NE;
2929 case ICMP_NE: return ICMP_EQ;
2930 case ICMP_UGT: return ICMP_ULE;
2931 case ICMP_ULT: return ICMP_UGE;
2932 case ICMP_UGE: return ICMP_ULT;
2933 case ICMP_ULE: return ICMP_UGT;
2934 case ICMP_SGT: return ICMP_SLE;
2935 case ICMP_SLT: return ICMP_SGE;
2936 case ICMP_SGE: return ICMP_SLT;
2937 case ICMP_SLE: return ICMP_SGT;
Reid Spencer45fb3f32006-11-20 01:22:35 +00002938
Dan Gohman7e2dd662008-05-31 02:47:54 +00002939 case FCMP_OEQ: return FCMP_UNE;
2940 case FCMP_ONE: return FCMP_UEQ;
2941 case FCMP_OGT: return FCMP_ULE;
2942 case FCMP_OLT: return FCMP_UGE;
2943 case FCMP_OGE: return FCMP_ULT;
2944 case FCMP_OLE: return FCMP_UGT;
2945 case FCMP_UEQ: return FCMP_ONE;
2946 case FCMP_UNE: return FCMP_OEQ;
2947 case FCMP_UGT: return FCMP_OLE;
2948 case FCMP_ULT: return FCMP_OGE;
2949 case FCMP_UGE: return FCMP_OLT;
2950 case FCMP_ULE: return FCMP_OGT;
2951 case FCMP_ORD: return FCMP_UNO;
2952 case FCMP_UNO: return FCMP_ORD;
2953 case FCMP_TRUE: return FCMP_FALSE;
2954 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencer45fb3f32006-11-20 01:22:35 +00002955 }
2956}
2957
Reid Spencere4d87aa2006-12-23 06:05:41 +00002958ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2959 switch (pred) {
Craig Topper50bee422012-02-05 22:14:15 +00002960 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00002961 case ICMP_EQ: case ICMP_NE:
2962 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2963 return pred;
2964 case ICMP_UGT: return ICMP_SGT;
2965 case ICMP_ULT: return ICMP_SLT;
2966 case ICMP_UGE: return ICMP_SGE;
2967 case ICMP_ULE: return ICMP_SLE;
2968 }
2969}
2970
Nick Lewycky4189a532008-01-28 03:48:02 +00002971ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2972 switch (pred) {
Craig Topper50bee422012-02-05 22:14:15 +00002973 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky4189a532008-01-28 03:48:02 +00002974 case ICMP_EQ: case ICMP_NE:
2975 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2976 return pred;
2977 case ICMP_SGT: return ICMP_UGT;
2978 case ICMP_SLT: return ICMP_ULT;
2979 case ICMP_SGE: return ICMP_UGE;
2980 case ICMP_SLE: return ICMP_ULE;
2981 }
2982}
2983
Reid Spencer3da43842007-02-28 22:00:54 +00002984/// Initialize a set of values that all satisfy the condition with C.
2985///
2986ConstantRange
2987ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2988 APInt Lower(C);
2989 APInt Upper(C);
2990 uint32_t BitWidth = C.getBitWidth();
2991 switch (pred) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002992 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer3da43842007-02-28 22:00:54 +00002993 case ICmpInst::ICMP_EQ: Upper++; break;
2994 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmanf21107c2010-01-26 16:04:20 +00002995 case ICmpInst::ICMP_ULT:
2996 Lower = APInt::getMinValue(BitWidth);
2997 // Check for an empty-set condition.
2998 if (Lower == Upper)
2999 return ConstantRange(BitWidth, /*isFullSet=*/false);
3000 break;
3001 case ICmpInst::ICMP_SLT:
3002 Lower = APInt::getSignedMinValue(BitWidth);
3003 // Check for an empty-set condition.
3004 if (Lower == Upper)
3005 return ConstantRange(BitWidth, /*isFullSet=*/false);
3006 break;
Reid Spencer3da43842007-02-28 22:00:54 +00003007 case ICmpInst::ICMP_UGT:
3008 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmanf21107c2010-01-26 16:04:20 +00003009 // Check for an empty-set condition.
3010 if (Lower == Upper)
3011 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer3da43842007-02-28 22:00:54 +00003012 break;
3013 case ICmpInst::ICMP_SGT:
3014 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmanf21107c2010-01-26 16:04:20 +00003015 // Check for an empty-set condition.
3016 if (Lower == Upper)
3017 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer3da43842007-02-28 22:00:54 +00003018 break;
3019 case ICmpInst::ICMP_ULE:
3020 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmanf21107c2010-01-26 16:04:20 +00003021 // Check for a full-set condition.
3022 if (Lower == Upper)
3023 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer3da43842007-02-28 22:00:54 +00003024 break;
3025 case ICmpInst::ICMP_SLE:
3026 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmanf21107c2010-01-26 16:04:20 +00003027 // Check for a full-set condition.
3028 if (Lower == Upper)
3029 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer3da43842007-02-28 22:00:54 +00003030 break;
3031 case ICmpInst::ICMP_UGE:
3032 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmanf21107c2010-01-26 16:04:20 +00003033 // Check for a full-set condition.
3034 if (Lower == Upper)
3035 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer3da43842007-02-28 22:00:54 +00003036 break;
3037 case ICmpInst::ICMP_SGE:
3038 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmanf21107c2010-01-26 16:04:20 +00003039 // Check for a full-set condition.
3040 if (Lower == Upper)
3041 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer3da43842007-02-28 22:00:54 +00003042 break;
3043 }
3044 return ConstantRange(Lower, Upper);
3045}
3046
Dan Gohman7e2dd662008-05-31 02:47:54 +00003047CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00003048 switch (pred) {
Craig Topper50bee422012-02-05 22:14:15 +00003049 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman7e2dd662008-05-31 02:47:54 +00003050 case ICMP_EQ: case ICMP_NE:
3051 return pred;
3052 case ICMP_SGT: return ICMP_SLT;
3053 case ICMP_SLT: return ICMP_SGT;
3054 case ICMP_SGE: return ICMP_SLE;
3055 case ICMP_SLE: return ICMP_SGE;
3056 case ICMP_UGT: return ICMP_ULT;
3057 case ICMP_ULT: return ICMP_UGT;
3058 case ICMP_UGE: return ICMP_ULE;
3059 case ICMP_ULE: return ICMP_UGE;
3060
Reid Spencer45fb3f32006-11-20 01:22:35 +00003061 case FCMP_FALSE: case FCMP_TRUE:
3062 case FCMP_OEQ: case FCMP_ONE:
3063 case FCMP_UEQ: case FCMP_UNE:
3064 case FCMP_ORD: case FCMP_UNO:
3065 return pred;
3066 case FCMP_OGT: return FCMP_OLT;
3067 case FCMP_OLT: return FCMP_OGT;
3068 case FCMP_OGE: return FCMP_OLE;
3069 case FCMP_OLE: return FCMP_OGE;
3070 case FCMP_UGT: return FCMP_ULT;
3071 case FCMP_ULT: return FCMP_UGT;
3072 case FCMP_UGE: return FCMP_ULE;
3073 case FCMP_ULE: return FCMP_UGE;
3074 }
3075}
3076
Reid Spencere4d87aa2006-12-23 06:05:41 +00003077bool CmpInst::isUnsigned(unsigned short predicate) {
3078 switch (predicate) {
3079 default: return false;
3080 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3081 case ICmpInst::ICMP_UGE: return true;
3082 }
3083}
3084
Nick Lewycky44df0232009-10-25 03:50:03 +00003085bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00003086 switch (predicate) {
3087 default: return false;
3088 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3089 case ICmpInst::ICMP_SGE: return true;
3090 }
3091}
3092
3093bool CmpInst::isOrdered(unsigned short predicate) {
3094 switch (predicate) {
3095 default: return false;
3096 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3097 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3098 case FCmpInst::FCMP_ORD: return true;
3099 }
3100}
3101
3102bool CmpInst::isUnordered(unsigned short predicate) {
3103 switch (predicate) {
3104 default: return false;
3105 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3106 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3107 case FCmpInst::FCMP_UNO: return true;
3108 }
3109}
3110
Nick Lewycky44df0232009-10-25 03:50:03 +00003111bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3112 switch(predicate) {
3113 default: return false;
3114 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3115 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3116 }
3117}
3118
3119bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3120 switch(predicate) {
3121 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3122 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3123 default: return false;
3124 }
3125}
3126
3127
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003128//===----------------------------------------------------------------------===//
3129// SwitchInst Implementation
3130//===----------------------------------------------------------------------===//
3131
Chris Lattneraa6e3502010-11-17 05:41:46 +00003132void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3133 assert(Value && Default && NumReserved);
3134 ReservedSpace = NumReserved;
Chris Lattnerb12261a2005-01-29 00:35:16 +00003135 NumOperands = 2;
Gabor Greifefe65362008-05-10 08:32:32 +00003136 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerb12261a2005-01-29 00:35:16 +00003137
Gabor Greif6c80c382008-05-26 21:33:52 +00003138 OperandList[0] = Value;
3139 OperandList[1] = Default;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003140}
3141
Chris Lattner910c80a2007-02-24 00:55:48 +00003142/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3143/// switch on and a default destination. The number of additional cases can
3144/// be specified here to make memory allocation more efficient. This
3145/// constructor can also autoinsert before another instruction.
3146SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3147 Instruction *InsertBefore)
Owen Anderson1d0be152009-08-13 21:58:54 +00003148 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3149 0, 0, InsertBefore) {
Chris Lattneraa6e3502010-11-17 05:41:46 +00003150 init(Value, Default, 2+NumCases*2);
Chris Lattner910c80a2007-02-24 00:55:48 +00003151}
3152
3153/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3154/// switch on and a default destination. The number of additional cases can
3155/// be specified here to make memory allocation more efficient. This
3156/// constructor also autoinserts at the end of the specified BasicBlock.
3157SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3158 BasicBlock *InsertAtEnd)
Owen Anderson1d0be152009-08-13 21:58:54 +00003159 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3160 0, 0, InsertAtEnd) {
Chris Lattneraa6e3502010-11-17 05:41:46 +00003161 init(Value, Default, 2+NumCases*2);
Chris Lattner910c80a2007-02-24 00:55:48 +00003162}
3163
Misha Brukmanfd939082005-04-21 23:48:37 +00003164SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattneraa6e3502010-11-17 05:41:46 +00003165 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3166 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3167 NumOperands = SI.getNumOperands();
Chris Lattnerb12261a2005-01-29 00:35:16 +00003168 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattneraa6e3502010-11-17 05:41:46 +00003169 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +00003170 OL[i] = InOL[i];
3171 OL[i+1] = InOL[i+1];
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003172 }
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00003173 TheSubsets = SI.TheSubsets;
Dan Gohman58cfa3b2009-08-25 22:11:20 +00003174 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003175}
3176
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00003177SwitchInst::~SwitchInst() {
Jay Foad1ed26ac2011-01-16 15:30:52 +00003178 dropHungoffUses();
Chris Lattnerb12261a2005-01-29 00:35:16 +00003179}
3180
3181
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003182/// addCase - Add an entry to the switch instruction...
3183///
Chris Lattnerd1a32602005-02-24 05:32:09 +00003184void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00003185 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00003186
3187 // FIXME: Currently we work with ConstantInt based cases.
3188 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00003189 Mapping.add(IntItem::fromConstantInt(OnVal));
3190 IntegersSubset CaseRanges = Mapping.getCase();
3191 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00003192}
3193
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00003194void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00003195 unsigned NewCaseIdx = getNumCases();
Chris Lattnerb12261a2005-01-29 00:35:16 +00003196 unsigned OpNo = NumOperands;
3197 if (OpNo+2 > ReservedSpace)
Jay Foad8891ed72011-04-01 08:00:58 +00003198 growOperands(); // Get more space!
Chris Lattnerb12261a2005-01-29 00:35:16 +00003199 // Initialize some new operands.
Chris Lattner1f377fc2005-01-29 01:05:12 +00003200 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerb12261a2005-01-29 00:35:16 +00003201 NumOperands = OpNo+2;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00003202
3203 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3204
3205 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3206 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00003207 Case.setSuccessor(Dest);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003208}
3209
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00003210/// removeCase - This method removes the specified case and its successor
3211/// from the switch instruction.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00003212void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00003213 unsigned idx = i.getCaseIndex();
3214
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00003215 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerb12261a2005-01-29 00:35:16 +00003216
3217 unsigned NumOps = getNumOperands();
3218 Use *OL = OperandList;
3219
Jay Foad0faa6092011-02-01 09:22:34 +00003220 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00003221 if (2 + (idx + 1) * 2 != NumOps) {
3222 OL[2 + idx * 2] = OL[NumOps - 2];
3223 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerb12261a2005-01-29 00:35:16 +00003224 }
3225
3226 // Nuke the last value.
3227 OL[NumOps-2].set(0);
3228 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00003229
3230 // Do the same with TheCases collection:
3231 if (i.SubsetIt != --TheSubsets.end()) {
3232 *i.SubsetIt = TheSubsets.back();
3233 TheSubsets.pop_back();
3234 } else {
3235 TheSubsets.pop_back();
3236 i.SubsetIt = TheSubsets.end();
3237 }
3238
Chris Lattnerb12261a2005-01-29 00:35:16 +00003239 NumOperands = NumOps-2;
3240}
3241
Jay Foad8891ed72011-04-01 08:00:58 +00003242/// growOperands - grow operands - This grows the operand list in response
3243/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerb12261a2005-01-29 00:35:16 +00003244///
Jay Foad8891ed72011-04-01 08:00:58 +00003245void SwitchInst::growOperands() {
Gabor Greifefe65362008-05-10 08:32:32 +00003246 unsigned e = getNumOperands();
Jay Foad8891ed72011-04-01 08:00:58 +00003247 unsigned NumOps = e*3;
Chris Lattnerb12261a2005-01-29 00:35:16 +00003248
3249 ReservedSpace = NumOps;
Gabor Greifefe65362008-05-10 08:32:32 +00003250 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerb12261a2005-01-29 00:35:16 +00003251 Use *OldOps = OperandList;
Gabor Greifefe65362008-05-10 08:32:32 +00003252 for (unsigned i = 0; i != e; ++i) {
Gabor Greif6c80c382008-05-26 21:33:52 +00003253 NewOps[i] = OldOps[i];
Chris Lattnerb12261a2005-01-29 00:35:16 +00003254 }
Chris Lattnerb12261a2005-01-29 00:35:16 +00003255 OperandList = NewOps;
Jay Foad1ed26ac2011-01-16 15:30:52 +00003256 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerb12261a2005-01-29 00:35:16 +00003257}
3258
3259
3260BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3261 return getSuccessor(idx);
3262}
3263unsigned SwitchInst::getNumSuccessorsV() const {
3264 return getNumSuccessors();
3265}
3266void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3267 setSuccessor(idx, B);
Alkis Evlogimenos91366a82004-07-29 12:33:25 +00003268}
Chris Lattner795948a2004-10-15 23:52:53 +00003269
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003270//===----------------------------------------------------------------------===//
Jay Foad1ed26ac2011-01-16 15:30:52 +00003271// IndirectBrInst Implementation
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003272//===----------------------------------------------------------------------===//
3273
Chris Lattnerab21db72009-10-28 00:19:10 +00003274void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands1df98592010-02-16 11:11:14 +00003275 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattnera4c206f2009-10-29 05:53:32 +00003276 "Address of indirectbr must be a pointer");
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003277 ReservedSpace = 1+NumDests;
3278 NumOperands = 1;
3279 OperandList = allocHungoffUses(ReservedSpace);
3280
3281 OperandList[0] = Address;
3282}
3283
3284
Jay Foad8891ed72011-04-01 08:00:58 +00003285/// growOperands - grow operands - This grows the operand list in response
3286/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003287///
Jay Foad8891ed72011-04-01 08:00:58 +00003288void IndirectBrInst::growOperands() {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003289 unsigned e = getNumOperands();
Jay Foad8891ed72011-04-01 08:00:58 +00003290 unsigned NumOps = e*2;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003291
3292 ReservedSpace = NumOps;
3293 Use *NewOps = allocHungoffUses(NumOps);
3294 Use *OldOps = OperandList;
3295 for (unsigned i = 0; i != e; ++i)
3296 NewOps[i] = OldOps[i];
3297 OperandList = NewOps;
Jay Foad1ed26ac2011-01-16 15:30:52 +00003298 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003299}
3300
Chris Lattnerab21db72009-10-28 00:19:10 +00003301IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3302 Instruction *InsertBefore)
3303: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003304 0, 0, InsertBefore) {
3305 init(Address, NumCases);
3306}
3307
Chris Lattnerab21db72009-10-28 00:19:10 +00003308IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3309 BasicBlock *InsertAtEnd)
3310: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003311 0, 0, InsertAtEnd) {
3312 init(Address, NumCases);
3313}
3314
Chris Lattnerab21db72009-10-28 00:19:10 +00003315IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3316 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003317 allocHungoffUses(IBI.getNumOperands()),
3318 IBI.getNumOperands()) {
3319 Use *OL = OperandList, *InOL = IBI.OperandList;
3320 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3321 OL[i] = InOL[i];
3322 SubclassOptionalData = IBI.SubclassOptionalData;
3323}
3324
Chris Lattnerab21db72009-10-28 00:19:10 +00003325IndirectBrInst::~IndirectBrInst() {
Jay Foad1ed26ac2011-01-16 15:30:52 +00003326 dropHungoffUses();
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003327}
3328
3329/// addDestination - Add a destination.
3330///
Chris Lattnerab21db72009-10-28 00:19:10 +00003331void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003332 unsigned OpNo = NumOperands;
3333 if (OpNo+1 > ReservedSpace)
Jay Foad8891ed72011-04-01 08:00:58 +00003334 growOperands(); // Get more space!
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003335 // Initialize some new operands.
3336 assert(OpNo < ReservedSpace && "Growing didn't work!");
3337 NumOperands = OpNo+1;
3338 OperandList[OpNo] = DestBB;
3339}
3340
3341/// removeDestination - This method removes the specified successor from the
Chris Lattnerab21db72009-10-28 00:19:10 +00003342/// indirectbr instruction.
3343void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003344 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3345
3346 unsigned NumOps = getNumOperands();
3347 Use *OL = OperandList;
3348
3349 // Replace this value with the last one.
3350 OL[idx+1] = OL[NumOps-1];
3351
3352 // Nuke the last value.
3353 OL[NumOps-1].set(0);
3354 NumOperands = NumOps-1;
3355}
3356
Chris Lattnerab21db72009-10-28 00:19:10 +00003357BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003358 return getSuccessor(idx);
3359}
Chris Lattnerab21db72009-10-28 00:19:10 +00003360unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003361 return getNumSuccessors();
3362}
Chris Lattnerab21db72009-10-28 00:19:10 +00003363void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003364 setSuccessor(idx, B);
3365}
3366
3367//===----------------------------------------------------------------------===//
Devang Patel50b6e332009-10-27 22:16:29 +00003368// clone_impl() implementations
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003369//===----------------------------------------------------------------------===//
3370
Chris Lattner795948a2004-10-15 23:52:53 +00003371// Define these methods here so vtables don't get emitted into every translation
3372// unit that uses these classes.
3373
Devang Patel50b6e332009-10-27 22:16:29 +00003374GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3375 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattner795948a2004-10-15 23:52:53 +00003376}
3377
Devang Patel50b6e332009-10-27 22:16:29 +00003378BinaryOperator *BinaryOperator::clone_impl() const {
3379 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattner795948a2004-10-15 23:52:53 +00003380}
3381
Devang Patel50b6e332009-10-27 22:16:29 +00003382FCmpInst* FCmpInst::clone_impl() const {
3383 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00003384}
3385
Devang Patel50b6e332009-10-27 22:16:29 +00003386ICmpInst* ICmpInst::clone_impl() const {
3387 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmane4569942008-05-23 00:36:11 +00003388}
3389
Devang Patel50b6e332009-10-27 22:16:29 +00003390ExtractValueInst *ExtractValueInst::clone_impl() const {
3391 return new ExtractValueInst(*this);
Owen Anderson333c4002009-07-09 23:48:35 +00003392}
3393
Devang Patel50b6e332009-10-27 22:16:29 +00003394InsertValueInst *InsertValueInst::clone_impl() const {
3395 return new InsertValueInst(*this);
Owen Anderson333c4002009-07-09 23:48:35 +00003396}
3397
Devang Patel50b6e332009-10-27 22:16:29 +00003398AllocaInst *AllocaInst::clone_impl() const {
3399 return new AllocaInst(getAllocatedType(),
3400 (Value*)getOperand(0),
3401 getAlignment());
Owen Anderson333c4002009-07-09 23:48:35 +00003402}
3403
Devang Patel50b6e332009-10-27 22:16:29 +00003404LoadInst *LoadInst::clone_impl() const {
Eli Friedman21006d42011-08-09 23:02:53 +00003405 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3406 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson333c4002009-07-09 23:48:35 +00003407}
3408
Devang Patel50b6e332009-10-27 22:16:29 +00003409StoreInst *StoreInst::clone_impl() const {
Eli Friedman1cc5e382011-08-10 17:39:11 +00003410 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman21006d42011-08-09 23:02:53 +00003411 getAlignment(), getOrdering(), getSynchScope());
3412
Owen Anderson333c4002009-07-09 23:48:35 +00003413}
3414
Eli Friedmanff030482011-07-28 21:48:00 +00003415AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3416 AtomicCmpXchgInst *Result =
3417 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3418 getOrdering(), getSynchScope());
3419 Result->setVolatile(isVolatile());
3420 return Result;
3421}
3422
3423AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3424 AtomicRMWInst *Result =
3425 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3426 getOrdering(), getSynchScope());
3427 Result->setVolatile(isVolatile());
3428 return Result;
3429}
3430
Eli Friedman47f35132011-07-25 23:16:38 +00003431FenceInst *FenceInst::clone_impl() const {
3432 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3433}
3434
Devang Patel50b6e332009-10-27 22:16:29 +00003435TruncInst *TruncInst::clone_impl() const {
3436 return new TruncInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003437}
3438
Devang Patel50b6e332009-10-27 22:16:29 +00003439ZExtInst *ZExtInst::clone_impl() const {
3440 return new ZExtInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003441}
3442
Devang Patel50b6e332009-10-27 22:16:29 +00003443SExtInst *SExtInst::clone_impl() const {
3444 return new SExtInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003445}
3446
Devang Patel50b6e332009-10-27 22:16:29 +00003447FPTruncInst *FPTruncInst::clone_impl() const {
3448 return new FPTruncInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003449}
3450
Devang Patel50b6e332009-10-27 22:16:29 +00003451FPExtInst *FPExtInst::clone_impl() const {
3452 return new FPExtInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003453}
3454
Devang Patel50b6e332009-10-27 22:16:29 +00003455UIToFPInst *UIToFPInst::clone_impl() const {
3456 return new UIToFPInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003457}
3458
Devang Patel50b6e332009-10-27 22:16:29 +00003459SIToFPInst *SIToFPInst::clone_impl() const {
3460 return new SIToFPInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003461}
3462
Devang Patel50b6e332009-10-27 22:16:29 +00003463FPToUIInst *FPToUIInst::clone_impl() const {
3464 return new FPToUIInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003465}
3466
Devang Patel50b6e332009-10-27 22:16:29 +00003467FPToSIInst *FPToSIInst::clone_impl() const {
3468 return new FPToSIInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003469}
3470
Devang Patel50b6e332009-10-27 22:16:29 +00003471PtrToIntInst *PtrToIntInst::clone_impl() const {
3472 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson333c4002009-07-09 23:48:35 +00003473}
3474
Devang Patel50b6e332009-10-27 22:16:29 +00003475IntToPtrInst *IntToPtrInst::clone_impl() const {
3476 return new IntToPtrInst(getOperand(0), getType());
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003477}
Owen Anderson333c4002009-07-09 23:48:35 +00003478
Devang Patel50b6e332009-10-27 22:16:29 +00003479BitCastInst *BitCastInst::clone_impl() const {
3480 return new BitCastInst(getOperand(0), getType());
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003481}
Reid Spencer3da59db2006-11-27 01:05:10 +00003482
Devang Patel50b6e332009-10-27 22:16:29 +00003483CallInst *CallInst::clone_impl() const {
3484 return new(getNumOperands()) CallInst(*this);
Owen Anderson333c4002009-07-09 23:48:35 +00003485}
3486
Devang Patel50b6e332009-10-27 22:16:29 +00003487SelectInst *SelectInst::clone_impl() const {
3488 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattner00f10232006-04-08 01:18:18 +00003489}
Owen Anderson333c4002009-07-09 23:48:35 +00003490
Devang Patel50b6e332009-10-27 22:16:29 +00003491VAArgInst *VAArgInst::clone_impl() const {
3492 return new VAArgInst(getOperand(0), getType());
Chris Lattner00f10232006-04-08 01:18:18 +00003493}
Owen Anderson333c4002009-07-09 23:48:35 +00003494
Devang Patel50b6e332009-10-27 22:16:29 +00003495ExtractElementInst *ExtractElementInst::clone_impl() const {
3496 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattner00f10232006-04-08 01:18:18 +00003497}
Owen Anderson333c4002009-07-09 23:48:35 +00003498
Devang Patel50b6e332009-10-27 22:16:29 +00003499InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner83694a92012-01-25 23:49:49 +00003500 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson333c4002009-07-09 23:48:35 +00003501}
3502
Devang Patel50b6e332009-10-27 22:16:29 +00003503ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner83694a92012-01-25 23:49:49 +00003504 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003505}
Owen Anderson333c4002009-07-09 23:48:35 +00003506
Devang Patel50b6e332009-10-27 22:16:29 +00003507PHINode *PHINode::clone_impl() const {
3508 return new PHINode(*this);
3509}
3510
Bill Wendlinge6e88262011-08-12 20:24:12 +00003511LandingPadInst *LandingPadInst::clone_impl() const {
3512 return new LandingPadInst(*this);
3513}
3514
Devang Patel50b6e332009-10-27 22:16:29 +00003515ReturnInst *ReturnInst::clone_impl() const {
3516 return new(getNumOperands()) ReturnInst(*this);
3517}
3518
3519BranchInst *BranchInst::clone_impl() const {
Jay Foad8e3914d2011-01-07 20:29:02 +00003520 return new(getNumOperands()) BranchInst(*this);
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003521}
Owen Anderson333c4002009-07-09 23:48:35 +00003522
Devang Patel50b6e332009-10-27 22:16:29 +00003523SwitchInst *SwitchInst::clone_impl() const {
3524 return new SwitchInst(*this);
Owen Anderson333c4002009-07-09 23:48:35 +00003525}
3526
Chris Lattnerab21db72009-10-28 00:19:10 +00003527IndirectBrInst *IndirectBrInst::clone_impl() const {
3528 return new IndirectBrInst(*this);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003529}
3530
3531
Devang Patel50b6e332009-10-27 22:16:29 +00003532InvokeInst *InvokeInst::clone_impl() const {
3533 return new(getNumOperands()) InvokeInst(*this);
Gabor Greifb1dbcd82008-05-15 10:04:30 +00003534}
Owen Anderson333c4002009-07-09 23:48:35 +00003535
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003536ResumeInst *ResumeInst::clone_impl() const {
3537 return new(1) ResumeInst(*this);
3538}
3539
Devang Patel50b6e332009-10-27 22:16:29 +00003540UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky67760642009-09-27 07:38:41 +00003541 LLVMContext &Context = getContext();
Devang Patel50b6e332009-10-27 22:16:29 +00003542 return new UnreachableInst(Context);
Owen Anderson333c4002009-07-09 23:48:35 +00003543}