blob: 0d6d23bb45db8275775747c7931af7a3e3dffabe [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
Pete Cooper1d078692015-12-14 20:29:16 +000090void PHINode::anchor() {}
91
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000093 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
94 ReservedSpace(PN.getNumOperands()) {
95 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000096 std::copy(PN.op_begin(), PN.op_end(), op_begin());
97 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101// removeIncomingValue - Remove an incoming value. This is useful if a
102// predecessor basic block is deleted.
103Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105
106 // Move everything after this operand down.
107 //
108 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
112 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000113
114 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000115 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000116 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117
118 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000121 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 eraseFromParent();
123 }
124 return Removed;
125}
126
Jay Foade98f29d2011-04-01 08:00:58 +0000127/// growOperands - grow operands - This grows the operand list in response
128/// to a push_back style of operation. This grows the number of ops by 1.5
129/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130///
Jay Foade98f29d2011-04-01 08:00:58 +0000131void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000132 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000133 unsigned NumOps = e + e / 2;
134 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
135
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000137 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Nate Begemanb3923212005-08-04 23:24:19 +0000140/// hasConstantValue - If the specified PHI node always merges together the same
141/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000142Value *PHINode::hasConstantValue() const {
143 // Exploit the fact that phi nodes always have at least one entry.
144 Value *ConstantValue = getIncomingValue(0);
145 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000146 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
147 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000148 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000149 // The case where the first value is this PHI.
150 ConstantValue = getIncomingValue(i);
151 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000152 if (ConstantValue == this)
153 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000155}
156
Bill Wendlingfae14752011-08-12 20:24:12 +0000157//===----------------------------------------------------------------------===//
158// LandingPadInst Implementation
159//===----------------------------------------------------------------------===//
160
David Majnemer7fddecc2015-06-17 20:52:32 +0000161LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
162 const Twine &NameStr, Instruction *InsertBefore)
163 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
164 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000165}
166
David Majnemer7fddecc2015-06-17 20:52:32 +0000167LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
168 const Twine &NameStr, BasicBlock *InsertAtEnd)
169 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
170 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000171}
172
173LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000174 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
175 LP.getNumOperands()),
176 ReservedSpace(LP.getNumOperands()) {
177 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000178 Use *OL = getOperandList();
179 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000180 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
181 OL[I] = InOL[I];
182
183 setCleanup(LP.isCleanup());
184}
185
David Majnemer7fddecc2015-06-17 20:52:32 +0000186LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000187 const Twine &NameStr,
188 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000189 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000190}
191
David Majnemer7fddecc2015-06-17 20:52:32 +0000192LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000193 const Twine &NameStr,
194 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000195 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000196}
197
David Majnemer7fddecc2015-06-17 20:52:32 +0000198void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000199 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000200 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000201 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000202 setName(NameStr);
203 setCleanup(false);
204}
205
206/// growOperands - grow operands - This grows the operand list in response to a
207/// push_back style of operation. This grows the number of ops by 2 times.
208void LandingPadInst::growOperands(unsigned Size) {
209 unsigned e = getNumOperands();
210 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000211 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000212 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000213}
214
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000215void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000216 unsigned OpNo = getNumOperands();
217 growOperands(1);
218 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000219 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000220 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000221}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000222
223//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000224// CallInst Implementation
225//===----------------------------------------------------------------------===//
226
Gordon Henriksen14a55692007-12-10 02:14:30 +0000227CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228}
229
David Blaikie348de692015-04-23 21:36:23 +0000230void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000231 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000232 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000233 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
234 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000235 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236
Jay Foad5bd375a2011-07-15 08:37:34 +0000237#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000238 assert((Args.size() == FTy->getNumParams() ||
239 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000240 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000241
242 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000243 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000244 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000245 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000246#endif
247
248 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000249
250 auto It = populateBundleOperandInfos(Bundles, Args.size());
251 (void)It;
252 assert(It + 1 == op_end() && "Should add up!");
253
Jay Foad5bd375a2011-07-15 08:37:34 +0000254 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255}
256
Jay Foad5bd375a2011-07-15 08:37:34 +0000257void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000258 FTy =
259 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000260 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000261 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000262
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000263 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000264
265 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266}
267
Daniel Dunbar4975db62009-07-25 04:41:11 +0000268CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 Instruction *InsertBefore)
270 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
271 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000272 Instruction::Call,
273 OperandTraits<CallInst>::op_end(this) - 1,
274 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000275 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276}
277
Daniel Dunbar4975db62009-07-25 04:41:11 +0000278CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279 BasicBlock *InsertAtEnd)
280 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
281 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 Instruction::Call,
283 OperandTraits<CallInst>::op_end(this) - 1,
284 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000285 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286}
287
Misha Brukmanb1c93172005-04-21 23:48:37 +0000288CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000289 : Instruction(CI.getType(), Instruction::Call,
290 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
291 CI.getNumOperands()),
292 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000293 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000294 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000295
Jay Foad5bd375a2011-07-15 08:37:34 +0000296 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000297 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
298 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000299 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Sanjoy Das2d161452015-11-18 06:23:38 +0000302CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
303 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000304 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000305
306 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
307 InsertPt);
308 NewCI->setTailCallKind(CI->getTailCallKind());
309 NewCI->setCallingConv(CI->getCallingConv());
310 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000311 NewCI->setAttributes(CI->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000312 return NewCI;
313}
314
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000315void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000316 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000317 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000318 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000319}
320
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000321void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
322 AttributeSet PAL = getAttributes();
323 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
324 setAttributes(PAL);
325}
326
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000327void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000328 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000329 AttrBuilder B(attr);
330 LLVMContext &Context = getContext();
331 PAL = PAL.removeAttributes(Context, i,
332 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000333 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000334}
335
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000336void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
337 AttributeSet PAL = getAttributes();
338 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
339 setAttributes(PAL);
340}
341
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000342void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
343 AttributeSet PAL = getAttributes();
344 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
345 setAttributes(PAL);
346}
347
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000348bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000349 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
350
Bill Wendling749a43d2012-12-30 13:50:49 +0000351 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000352 return true;
353 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000354 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000355 return false;
356}
357
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000358bool CallInst::dataOperandHasImpliedAttr(unsigned i,
359 Attribute::AttrKind A) const {
360
Sanjoy Das776e4a72015-11-05 01:53:26 +0000361 // There are getNumOperands() - 1 data operands. The last operand is the
362 // callee.
363 assert(i < getNumOperands() && "Data operand index out of bounds!");
364
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000365 // The attribute A can either be directly specified, if the operand in
366 // question is a call argument; or be indirectly implied by the kind of its
367 // containing operand bundle, if the operand is a bundle operand.
368
369 if (i < (getNumArgOperands() + 1))
370 return paramHasAttr(i, A);
371
372 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
373 "Must be either a call argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000374 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000375}
376
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000377/// IsConstantOne - Return true only if val is constant int 1
378static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000379 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000380 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
381 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000382}
383
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000384static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000385 BasicBlock *InsertAtEnd, Type *IntPtrTy,
386 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000387 Value *ArraySize, Function *MallocF,
388 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000389 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000390 "createMalloc needs either InsertBefore or InsertAtEnd");
391
392 // malloc(type) becomes:
393 // bitcast (i8* malloc(typeSize)) to type*
394 // malloc(type, arraySize) becomes:
395 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000396 if (!ArraySize)
397 ArraySize = ConstantInt::get(IntPtrTy, 1);
398 else if (ArraySize->getType() != IntPtrTy) {
399 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000400 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
401 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000402 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000403 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
404 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000405 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000407 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408 if (IsConstantOne(AllocSize)) {
409 AllocSize = ArraySize; // Operand * 1 = Operand
410 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
411 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
412 false /*ZExt*/);
413 // Malloc arg is constant product of type size and array size
414 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
415 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000416 // Multiply type size by the array size...
417 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000418 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
419 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000420 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000421 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
422 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000423 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000424 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425
Victor Hernandez788eaab2009-09-18 19:20:02 +0000426 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427 // Create the call to Malloc.
428 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
429 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000430 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000431 Value *MallocFunc = MallocF;
432 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000433 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000434 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000435 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000436 CallInst *MCall = nullptr;
437 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000438 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000439 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000440 Result = MCall;
441 if (Result->getType() != AllocPtrType)
442 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000443 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000444 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000445 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000446 Result = MCall;
447 if (Result->getType() != AllocPtrType) {
448 InsertAtEnd->getInstList().push_back(MCall);
449 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000450 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000451 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000452 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000453 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000454 if (Function *F = dyn_cast<Function>(MallocFunc)) {
455 MCall->setCallingConv(F->getCallingConv());
456 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
457 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000458 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000459
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000460 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461}
462
463/// CreateMalloc - Generate the IR for a call to malloc:
464/// 1. Compute the malloc call's argument as the specified type's size,
465/// possibly multiplied by the array size if the array size is not
466/// constant 1.
467/// 2. Call malloc with that argument.
468/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000469Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000470 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000472 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000474 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000475 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000476}
477
478/// CreateMalloc - Generate the IR for a call to malloc:
479/// 1. Compute the malloc call's argument as the specified type's size,
480/// possibly multiplied by the array size if the array size is not
481/// constant 1.
482/// 2. Call malloc with that argument.
483/// 3. Bitcast the result of the malloc call to the specified type.
484/// Note: This function does not add the bitcast to the basic block, that is the
485/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000486Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000487 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000488 Value *AllocSize, Value *ArraySize,
489 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000490 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000491 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000493
Victor Hernandeze2971492009-10-24 04:23:03 +0000494static Instruction* createFree(Value* Source, Instruction *InsertBefore,
495 BasicBlock *InsertAtEnd) {
496 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
497 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000498 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000499 "Can not free something of nonpointer type!");
500
501 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
502 Module* M = BB->getParent()->getParent();
503
Chris Lattner229907c2011-07-18 04:54:35 +0000504 Type *VoidTy = Type::getVoidTy(M->getContext());
505 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000506 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000507 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000508 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000509 Value *PtrCast = Source;
510 if (InsertBefore) {
511 if (Source->getType() != IntPtrTy)
512 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
513 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
514 } else {
515 if (Source->getType() != IntPtrTy)
516 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
517 Result = CallInst::Create(FreeFunc, PtrCast, "");
518 }
519 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000520 if (Function *F = dyn_cast<Function>(FreeFunc))
521 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000522
523 return Result;
524}
525
526/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000527Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000528 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000529}
530
531/// CreateFree - Generate the IR for a call to the builtin free function.
532/// Note: This function does not add the call to the basic block, that is the
533/// responsibility of the caller.
534Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000535 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000536 assert(FreeCall && "CreateFree did not create a CallInst");
537 return FreeCall;
538}
539
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540//===----------------------------------------------------------------------===//
541// InvokeInst Implementation
542//===----------------------------------------------------------------------===//
543
David Blaikie3e807092015-05-13 18:35:26 +0000544void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
545 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000546 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000547 const Twine &NameStr) {
548 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000549
Sanjoy Das9303c242015-09-24 19:14:18 +0000550 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
551 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000552 Op<-3>() = Fn;
553 Op<-2>() = IfNormal;
554 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000555
556#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000557 assert(((Args.size() == FTy->getNumParams()) ||
558 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000559 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000560
Jay Foad5bd375a2011-07-15 08:37:34 +0000561 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000562 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000563 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000564 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000565#endif
566
567 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000568
569 auto It = populateBundleOperandInfos(Bundles, Args.size());
570 (void)It;
571 assert(It + 3 == op_end() && "Should add up!");
572
Jay Foad5bd375a2011-07-15 08:37:34 +0000573 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000574}
575
Misha Brukmanb1c93172005-04-21 23:48:37 +0000576InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000577 : TerminatorInst(II.getType(), Instruction::Invoke,
578 OperandTraits<InvokeInst>::op_end(this) -
579 II.getNumOperands(),
580 II.getNumOperands()),
581 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000582 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000583 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000584 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
585 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000586 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000587}
588
Sanjoy Das2d161452015-11-18 06:23:38 +0000589InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
590 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000591 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000592
593 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
594 II->getUnwindDest(), Args, OpB,
595 II->getName(), InsertPt);
596 NewII->setCallingConv(II->getCallingConv());
597 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000598 NewII->setAttributes(II->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000599 return NewII;
600}
601
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
603 return getSuccessor(idx);
604}
605unsigned InvokeInst::getNumSuccessorsV() const {
606 return getNumSuccessors();
607}
608void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
609 return setSuccessor(idx, B);
610}
611
Michael Gottesman41748d72013-06-27 00:25:01 +0000612bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000613 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000614 return true;
Sanjoy Das98a341b2015-10-22 03:12:22 +0000615
616 // Operand bundles override attributes on the called function, but don't
617 // override attributes directly present on the invoke instruction.
618 if (isFnAttrDisallowedByOpBundle(A))
619 return false;
620
Bill Wendling375eb1f2012-10-09 00:28:54 +0000621 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000622 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000623 return false;
624}
625
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000626bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000627 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
628
Bill Wendling749a43d2012-12-30 13:50:49 +0000629 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000630 return true;
631 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000632 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000633 return false;
634}
635
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000636bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
637 Attribute::AttrKind A) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000638 // There are getNumOperands() - 3 data operands. The last three operands are
639 // the callee and the two successor basic blocks.
640 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
641
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000642 // The attribute A can either be directly specified, if the operand in
643 // question is an invoke argument; or be indirectly implied by the kind of its
644 // containing operand bundle, if the operand is a bundle operand.
645
646 if (i < (getNumArgOperands() + 1))
647 return paramHasAttr(i, A);
648
649 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
650 "Must be either an invoke argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000651 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000652}
653
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000654void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000655 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000656 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000657 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000658}
659
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000660void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000661 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000662 AttrBuilder B(attr);
663 PAL = PAL.removeAttributes(getContext(), i,
664 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000665 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000666}
667
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000668void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
669 AttributeSet PAL = getAttributes();
670 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
671 setAttributes(PAL);
672}
673
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000674void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
675 AttributeSet PAL = getAttributes();
676 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
677 setAttributes(PAL);
678}
679
Bill Wendlingfae14752011-08-12 20:24:12 +0000680LandingPadInst *InvokeInst::getLandingPadInst() const {
681 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
682}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000683
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684//===----------------------------------------------------------------------===//
685// ReturnInst Implementation
686//===----------------------------------------------------------------------===//
687
Chris Lattner2195fc42007-02-24 00:55:48 +0000688ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000689 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000690 OperandTraits<ReturnInst>::op_end(this) -
691 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000692 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000693 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000694 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000695 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000696}
697
Owen Anderson55f1c092009-08-13 21:58:54 +0000698ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
699 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000700 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
701 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000702 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000703 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
Owen Anderson55f1c092009-08-13 21:58:54 +0000705ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
706 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000707 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
708 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000709 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000710 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000711}
Owen Anderson55f1c092009-08-13 21:58:54 +0000712ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
713 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000714 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000715}
716
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717unsigned ReturnInst::getNumSuccessorsV() const {
718 return getNumSuccessors();
719}
720
Devang Patelae682fb2008-02-26 17:56:20 +0000721/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
722/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000724 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000728 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000729}
730
Devang Patel59643e52008-02-23 00:35:18 +0000731ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000732}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000733
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000735// ResumeInst Implementation
736//===----------------------------------------------------------------------===//
737
738ResumeInst::ResumeInst(const ResumeInst &RI)
739 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
740 OperandTraits<ResumeInst>::op_begin(this), 1) {
741 Op<0>() = RI.Op<0>();
742}
743
744ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
745 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
746 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
747 Op<0>() = Exn;
748}
749
750ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
751 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
752 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
753 Op<0>() = Exn;
754}
755
756unsigned ResumeInst::getNumSuccessorsV() const {
757 return getNumSuccessors();
758}
759
760void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
761 llvm_unreachable("ResumeInst has no successors!");
762}
763
764BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
765 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000766}
767
768//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000769// CleanupReturnInst Implementation
770//===----------------------------------------------------------------------===//
771
772CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
773 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
774 OperandTraits<CleanupReturnInst>::op_end(this) -
775 CRI.getNumOperands(),
776 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000777 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000778 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000779 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000780 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000781}
782
David Majnemer8a1c45d2015-12-12 05:38:55 +0000783void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000784 if (UnwindBB)
785 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000786
David Majnemer8a1c45d2015-12-12 05:38:55 +0000787 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000788 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000789 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000790}
791
David Majnemer8a1c45d2015-12-12 05:38:55 +0000792CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
793 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000794 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
795 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000796 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
797 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000798 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000799}
800
David Majnemer8a1c45d2015-12-12 05:38:55 +0000801CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
802 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000803 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
804 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000805 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
806 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000807 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000808}
809
810BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
811 assert(Idx == 0);
812 return getUnwindDest();
813}
814unsigned CleanupReturnInst::getNumSuccessorsV() const {
815 return getNumSuccessors();
816}
817void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
818 assert(Idx == 0);
819 setUnwindDest(B);
820}
821
822//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000823// CatchReturnInst Implementation
824//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000825void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000826 Op<0>() = CatchPad;
827 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000828}
David Majnemer654e1302015-07-31 17:58:14 +0000829
830CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
831 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000832 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
833 Op<0>() = CRI.Op<0>();
834 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000835}
836
David Majnemer8a1c45d2015-12-12 05:38:55 +0000837CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000838 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000839 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000840 OperandTraits<CatchReturnInst>::op_begin(this), 2,
841 InsertBefore) {
842 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000843}
844
David Majnemer8a1c45d2015-12-12 05:38:55 +0000845CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000846 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000847 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000848 OperandTraits<CatchReturnInst>::op_begin(this), 2,
849 InsertAtEnd) {
850 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000851}
852
853BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000854 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000855 return getSuccessor();
856}
857unsigned CatchReturnInst::getNumSuccessorsV() const {
858 return getNumSuccessors();
859}
860void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000861 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000862 setSuccessor(B);
863}
864
865//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000866// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000867//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000868
869CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
870 unsigned NumReservedValues,
871 const Twine &NameStr,
872 Instruction *InsertBefore)
873 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
874 InsertBefore) {
875 if (UnwindDest)
876 ++NumReservedValues;
877 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000878 setName(NameStr);
879}
880
David Majnemer8a1c45d2015-12-12 05:38:55 +0000881CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
882 unsigned NumReservedValues,
883 const Twine &NameStr, BasicBlock *InsertAtEnd)
884 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000885 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000886 if (UnwindDest)
887 ++NumReservedValues;
888 init(ParentPad, UnwindDest, NumReservedValues + 1);
889 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000890}
891
David Majnemer8a1c45d2015-12-12 05:38:55 +0000892CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
893 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
894 CSI.getNumOperands()) {
895 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
896 setNumHungOffUseOperands(ReservedSpace);
897 Use *OL = getOperandList();
898 const Use *InOL = CSI.getOperandList();
899 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
900 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000901}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000902
903void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
904 unsigned NumReservedValues) {
905 assert(ParentPad && NumReservedValues);
906
907 ReservedSpace = NumReservedValues;
908 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
909 allocHungoffUses(ReservedSpace);
910
911 Op<0>() = ParentPad;
912 if (UnwindDest) {
913 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
914 setUnwindDest(UnwindDest);
915 }
916}
917
918/// growOperands - grow operands - This grows the operand list in response to a
919/// push_back style of operation. This grows the number of ops by 2 times.
920void CatchSwitchInst::growOperands(unsigned Size) {
921 unsigned NumOperands = getNumOperands();
922 assert(NumOperands >= 1);
923 if (ReservedSpace >= NumOperands + Size)
924 return;
925 ReservedSpace = (NumOperands + Size / 2) * 2;
926 growHungoffUses(ReservedSpace);
927}
928
929void CatchSwitchInst::addHandler(BasicBlock *Handler) {
930 unsigned OpNo = getNumOperands();
931 growOperands(1);
932 assert(OpNo < ReservedSpace && "Growing didn't work!");
933 setNumHungOffUseOperands(getNumOperands() + 1);
934 getOperandList()[OpNo] = Handler;
935}
936
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000937void CatchSwitchInst::removeHandler(handler_iterator HI) {
938 // Move all subsequent handlers up one.
939 Use *EndDst = op_end() - 1;
940 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
941 *CurDst = *(CurDst + 1);
942 // Null out the last handler use.
943 *EndDst = nullptr;
944
945 setNumHungOffUseOperands(getNumOperands() - 1);
946}
947
David Majnemer8a1c45d2015-12-12 05:38:55 +0000948BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
949 return getSuccessor(idx);
950}
951unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +0000952 return getNumSuccessors();
953}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000954void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
955 setSuccessor(idx, B);
956}
957
958//===----------------------------------------------------------------------===//
959// FuncletPadInst Implementation
960//===----------------------------------------------------------------------===//
961void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
962 const Twine &NameStr) {
963 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
964 std::copy(Args.begin(), Args.end(), op_begin());
965 setParentPad(ParentPad);
966 setName(NameStr);
967}
968
969FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
970 : Instruction(FPI.getType(), FPI.getOpcode(),
971 OperandTraits<FuncletPadInst>::op_end(this) -
972 FPI.getNumOperands(),
973 FPI.getNumOperands()) {
974 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
975 setParentPad(FPI.getParentPad());
976}
977
978FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
979 ArrayRef<Value *> Args, unsigned Values,
980 const Twine &NameStr, Instruction *InsertBefore)
981 : Instruction(ParentPad->getType(), Op,
982 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
983 InsertBefore) {
984 init(ParentPad, Args, NameStr);
985}
986
987FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
988 ArrayRef<Value *> Args, unsigned Values,
989 const Twine &NameStr, BasicBlock *InsertAtEnd)
990 : Instruction(ParentPad->getType(), Op,
991 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
992 InsertAtEnd) {
993 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000994}
995
996//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000997// UnreachableInst Implementation
998//===----------------------------------------------------------------------===//
999
Owen Anderson55f1c092009-08-13 21:58:54 +00001000UnreachableInst::UnreachableInst(LLVMContext &Context,
1001 Instruction *InsertBefore)
1002 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001003 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001004}
Owen Anderson55f1c092009-08-13 21:58:54 +00001005UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1006 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001007 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001008}
1009
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001010unsigned UnreachableInst::getNumSuccessorsV() const {
1011 return getNumSuccessors();
1012}
1013
1014void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001015 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001016}
1017
1018BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001019 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001020}
1021
1022//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001023// BranchInst Implementation
1024//===----------------------------------------------------------------------===//
1025
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001026void BranchInst::AssertOK() {
1027 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001028 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001029 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001030}
1031
Chris Lattner2195fc42007-02-24 00:55:48 +00001032BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001033 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001034 OperandTraits<BranchInst>::op_end(this) - 1,
1035 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001036 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001037 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001038}
1039BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1040 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001041 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001042 OperandTraits<BranchInst>::op_end(this) - 3,
1043 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001044 Op<-1>() = IfTrue;
1045 Op<-2>() = IfFalse;
1046 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001047#ifndef NDEBUG
1048 AssertOK();
1049#endif
1050}
1051
1052BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001053 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001054 OperandTraits<BranchInst>::op_end(this) - 1,
1055 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001056 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001057 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001058}
1059
1060BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1061 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001062 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001063 OperandTraits<BranchInst>::op_end(this) - 3,
1064 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001065 Op<-1>() = IfTrue;
1066 Op<-2>() = IfFalse;
1067 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001068#ifndef NDEBUG
1069 AssertOK();
1070#endif
1071}
1072
1073
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001074BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001075 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001076 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1077 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001078 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001079 if (BI.getNumOperands() != 1) {
1080 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001081 Op<-3>() = BI.Op<-3>();
1082 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001083 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001084 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001085}
1086
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001087void BranchInst::swapSuccessors() {
1088 assert(isConditional() &&
1089 "Cannot swap successors of an unconditional branch");
1090 Op<-1>().swap(Op<-2>());
1091
1092 // Update profile metadata if present and it matches our structural
1093 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001094 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001095 if (!ProfileData || ProfileData->getNumOperands() != 3)
1096 return;
1097
1098 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001099 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1100 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001101 setMetadata(LLVMContext::MD_prof,
1102 MDNode::get(ProfileData->getContext(), Ops));
1103}
1104
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001105BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1106 return getSuccessor(idx);
1107}
1108unsigned BranchInst::getNumSuccessorsV() const {
1109 return getNumSuccessors();
1110}
1111void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1112 setSuccessor(idx, B);
1113}
1114
1115
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001116//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001117// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001118//===----------------------------------------------------------------------===//
1119
Owen Andersonb6b25302009-07-14 23:09:55 +00001120static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001121 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001122 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001123 else {
1124 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001125 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001126 assert(Amt->getType()->isIntegerTy() &&
1127 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001128 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001129 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001130}
1131
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001132AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1133 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001134
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001135AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1136 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001137
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001138AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001139 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001140 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001141
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001142AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001143 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001144 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001145
Chris Lattner229907c2011-07-18 04:54:35 +00001146AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001147 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001148 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1149 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1150 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001151 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001152 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001153 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001154}
1155
Chris Lattner229907c2011-07-18 04:54:35 +00001156AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001157 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001158 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1159 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1160 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001161 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001162 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001163 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001164}
1165
Gordon Henriksen14a55692007-12-10 02:14:30 +00001166// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001167AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001168}
1169
Victor Hernandez8acf2952009-10-23 21:09:37 +00001170void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001171 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001172 assert(Align <= MaximumAlignment &&
1173 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001174 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1175 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001176 assert(getAlignment() == Align && "Alignment representation error!");
1177}
1178
Victor Hernandez8acf2952009-10-23 21:09:37 +00001179bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001180 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001181 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001182 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001183}
1184
Chris Lattner8b291e62008-11-26 02:54:17 +00001185/// isStaticAlloca - Return true if this alloca is in the entry block of the
1186/// function and is a constant size. If so, the code generator will fold it
1187/// into the prolog/epilog code, so it is basically free.
1188bool AllocaInst::isStaticAlloca() const {
1189 // Must be constant size.
1190 if (!isa<ConstantInt>(getArraySize())) return false;
1191
1192 // Must be in the entry block.
1193 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001194 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001195}
1196
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001197//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001198// LoadInst Implementation
1199//===----------------------------------------------------------------------===//
1200
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001201void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001202 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001203 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001204 assert(!(isAtomic() && getAlignment() == 0) &&
1205 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001206}
1207
Daniel Dunbar4975db62009-07-25 04:41:11 +00001208LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001209 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001210
Daniel Dunbar4975db62009-07-25 04:41:11 +00001211LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001212 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001213
David Blaikie0c28fd72015-05-20 21:46:30 +00001214LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001215 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001216 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001217
1218LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1219 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001220 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001221
David Blaikieb7a029872015-04-17 19:56:21 +00001222LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001223 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001224 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001225 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001226
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001227LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001228 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001229 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001230}
1231
David Blaikie15d9a4c2015-04-06 20:59:48 +00001232LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001233 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001234 SynchronizationScope SynchScope, Instruction *InsertBef)
1235 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001236 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001237 setVolatile(isVolatile);
1238 setAlignment(Align);
1239 setAtomic(Order, SynchScope);
1240 AssertOK();
1241 setName(Name);
1242}
1243
1244LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1245 unsigned Align, AtomicOrdering Order,
1246 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001247 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001248 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001249 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001250 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001251 setAlignment(Align);
1252 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001253 AssertOK();
1254 setName(Name);
1255}
1256
Daniel Dunbar27096822009-08-11 18:11:15 +00001257LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1258 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1259 Load, Ptr, InsertBef) {
1260 setVolatile(false);
1261 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001262 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001263 AssertOK();
1264 if (Name && Name[0]) setName(Name);
1265}
1266
1267LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1268 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1269 Load, Ptr, InsertAE) {
1270 setVolatile(false);
1271 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001272 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001273 AssertOK();
1274 if (Name && Name[0]) setName(Name);
1275}
1276
David Blaikie0c28fd72015-05-20 21:46:30 +00001277LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001278 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001279 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1280 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001281 setVolatile(isVolatile);
1282 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001283 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001284 AssertOK();
1285 if (Name && Name[0]) setName(Name);
1286}
1287
1288LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1289 BasicBlock *InsertAE)
1290 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1291 Load, Ptr, InsertAE) {
1292 setVolatile(isVolatile);
1293 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001294 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001295 AssertOK();
1296 if (Name && Name[0]) setName(Name);
1297}
1298
Christopher Lamb84485702007-04-22 19:24:39 +00001299void LoadInst::setAlignment(unsigned Align) {
1300 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001301 assert(Align <= MaximumAlignment &&
1302 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001303 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001304 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001305 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001306}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001307
1308//===----------------------------------------------------------------------===//
1309// StoreInst Implementation
1310//===----------------------------------------------------------------------===//
1311
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001312void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001313 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001314 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001315 "Ptr must have pointer type!");
1316 assert(getOperand(0)->getType() ==
1317 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001318 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001319 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001320 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001321}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001322
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001323StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001324 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001325
1326StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001327 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001328
Misha Brukmanb1c93172005-04-21 23:48:37 +00001329StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001330 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001331 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001332
1333StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001334 BasicBlock *InsertAtEnd)
1335 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1336
1337StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1338 Instruction *InsertBefore)
1339 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1340 InsertBefore) {}
1341
1342StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1343 BasicBlock *InsertAtEnd)
1344 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1345 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001346
Misha Brukmanb1c93172005-04-21 23:48:37 +00001347StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001348 unsigned Align, AtomicOrdering Order,
1349 SynchronizationScope SynchScope,
1350 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001351 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001352 OperandTraits<StoreInst>::op_begin(this),
1353 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001354 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001355 Op<0>() = val;
1356 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001357 setVolatile(isVolatile);
1358 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001359 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001360 AssertOK();
1361}
1362
1363StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001364 unsigned Align, AtomicOrdering Order,
1365 SynchronizationScope SynchScope,
1366 BasicBlock *InsertAtEnd)
1367 : Instruction(Type::getVoidTy(val->getContext()), Store,
1368 OperandTraits<StoreInst>::op_begin(this),
1369 OperandTraits<StoreInst>::operands(this),
1370 InsertAtEnd) {
1371 Op<0>() = val;
1372 Op<1>() = addr;
1373 setVolatile(isVolatile);
1374 setAlignment(Align);
1375 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001376 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001377}
1378
Christopher Lamb84485702007-04-22 19:24:39 +00001379void StoreInst::setAlignment(unsigned Align) {
1380 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001381 assert(Align <= MaximumAlignment &&
1382 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001383 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001384 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001385 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001386}
1387
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001388//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001389// AtomicCmpXchgInst Implementation
1390//===----------------------------------------------------------------------===//
1391
1392void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001393 AtomicOrdering SuccessOrdering,
1394 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001395 SynchronizationScope SynchScope) {
1396 Op<0>() = Ptr;
1397 Op<1>() = Cmp;
1398 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001399 setSuccessOrdering(SuccessOrdering);
1400 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001401 setSynchScope(SynchScope);
1402
1403 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1404 "All operands must be non-null!");
1405 assert(getOperand(0)->getType()->isPointerTy() &&
1406 "Ptr must have pointer type!");
1407 assert(getOperand(1)->getType() ==
1408 cast<PointerType>(getOperand(0)->getType())->getElementType()
1409 && "Ptr must be a pointer to Cmp type!");
1410 assert(getOperand(2)->getType() ==
1411 cast<PointerType>(getOperand(0)->getType())->getElementType()
1412 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001413 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001414 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001415 assert(FailureOrdering != NotAtomic &&
1416 "AtomicCmpXchg instructions must be atomic!");
1417 assert(SuccessOrdering >= FailureOrdering &&
1418 "AtomicCmpXchg success ordering must be at least as strong as fail");
1419 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1420 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001421}
1422
1423AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001424 AtomicOrdering SuccessOrdering,
1425 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001426 SynchronizationScope SynchScope,
1427 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001428 : Instruction(
1429 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1430 nullptr),
1431 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1432 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001433 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001434}
1435
1436AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001437 AtomicOrdering SuccessOrdering,
1438 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001439 SynchronizationScope SynchScope,
1440 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001441 : Instruction(
1442 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1443 nullptr),
1444 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1445 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001446 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001447}
Tim Northover420a2162014-06-13 14:24:07 +00001448
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001449//===----------------------------------------------------------------------===//
1450// AtomicRMWInst Implementation
1451//===----------------------------------------------------------------------===//
1452
1453void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1454 AtomicOrdering Ordering,
1455 SynchronizationScope SynchScope) {
1456 Op<0>() = Ptr;
1457 Op<1>() = Val;
1458 setOperation(Operation);
1459 setOrdering(Ordering);
1460 setSynchScope(SynchScope);
1461
1462 assert(getOperand(0) && getOperand(1) &&
1463 "All operands must be non-null!");
1464 assert(getOperand(0)->getType()->isPointerTy() &&
1465 "Ptr must have pointer type!");
1466 assert(getOperand(1)->getType() ==
1467 cast<PointerType>(getOperand(0)->getType())->getElementType()
1468 && "Ptr must be a pointer to Val type!");
1469 assert(Ordering != NotAtomic &&
1470 "AtomicRMW instructions must be atomic!");
1471}
1472
1473AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1474 AtomicOrdering Ordering,
1475 SynchronizationScope SynchScope,
1476 Instruction *InsertBefore)
1477 : Instruction(Val->getType(), AtomicRMW,
1478 OperandTraits<AtomicRMWInst>::op_begin(this),
1479 OperandTraits<AtomicRMWInst>::operands(this),
1480 InsertBefore) {
1481 Init(Operation, Ptr, Val, Ordering, SynchScope);
1482}
1483
1484AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1485 AtomicOrdering Ordering,
1486 SynchronizationScope SynchScope,
1487 BasicBlock *InsertAtEnd)
1488 : Instruction(Val->getType(), AtomicRMW,
1489 OperandTraits<AtomicRMWInst>::op_begin(this),
1490 OperandTraits<AtomicRMWInst>::operands(this),
1491 InsertAtEnd) {
1492 Init(Operation, Ptr, Val, Ordering, SynchScope);
1493}
1494
1495//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001496// FenceInst Implementation
1497//===----------------------------------------------------------------------===//
1498
1499FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1500 SynchronizationScope SynchScope,
1501 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001502 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001503 setOrdering(Ordering);
1504 setSynchScope(SynchScope);
1505}
1506
1507FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1508 SynchronizationScope SynchScope,
1509 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001510 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001511 setOrdering(Ordering);
1512 setSynchScope(SynchScope);
1513}
1514
1515//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001516// GetElementPtrInst Implementation
1517//===----------------------------------------------------------------------===//
1518
Pete Cooper1d078692015-12-14 20:29:16 +00001519void GetElementPtrInst::anchor() {}
1520
Jay Foadd1b78492011-07-25 09:48:08 +00001521void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001522 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001523 assert(getNumOperands() == 1 + IdxList.size() &&
1524 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001525 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001526 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001527 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001528}
1529
Gabor Greiff6caff662008-05-10 08:32:32 +00001530GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001531 : Instruction(GEPI.getType(), GetElementPtr,
1532 OperandTraits<GetElementPtrInst>::op_end(this) -
1533 GEPI.getNumOperands(),
1534 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001535 SourceElementType(GEPI.SourceElementType),
1536 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001537 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001538 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001539}
1540
Chris Lattner6090a422009-03-09 04:46:40 +00001541/// getIndexedType - Returns the type of the element that would be accessed with
1542/// a gep instruction with the specified parameters.
1543///
1544/// The Idxs pointer should point to a continuous piece of memory containing the
1545/// indices, either as Value* or uint64_t.
1546///
1547/// A null type is returned if the indices are invalid for the specified
1548/// pointer type.
1549///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001550template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001551static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001552 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001553 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001554 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001555
Chris Lattner6090a422009-03-09 04:46:40 +00001556 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001557 // it cannot be 'stepped over'.
1558 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001559 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001560
Dan Gohman1ecaf452008-05-31 00:58:22 +00001561 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001562 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001563 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001564 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001565 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001566 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001567 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001568 }
Craig Topperc6207612014-04-09 06:08:46 +00001569 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001570}
1571
David Blaikied288fb82015-03-30 21:41:43 +00001572Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1573 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001574}
1575
David Blaikied288fb82015-03-30 21:41:43 +00001576Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001577 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001578 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001579}
1580
David Blaikied288fb82015-03-30 21:41:43 +00001581Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1582 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001583}
1584
Chris Lattner45f15572007-04-14 00:12:57 +00001585/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1586/// zeros. If so, the result pointer and the first operand have the same
1587/// value, just potentially different types.
1588bool GetElementPtrInst::hasAllZeroIndices() const {
1589 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1590 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1591 if (!CI->isZero()) return false;
1592 } else {
1593 return false;
1594 }
1595 }
1596 return true;
1597}
1598
Chris Lattner27058292007-04-27 20:35:56 +00001599/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1600/// constant integers. If so, the result pointer and the first operand have
1601/// a constant offset between them.
1602bool GetElementPtrInst::hasAllConstantIndices() const {
1603 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1604 if (!isa<ConstantInt>(getOperand(i)))
1605 return false;
1606 }
1607 return true;
1608}
1609
Dan Gohman1b849082009-09-07 23:54:19 +00001610void GetElementPtrInst::setIsInBounds(bool B) {
1611 cast<GEPOperator>(this)->setIsInBounds(B);
1612}
Chris Lattner45f15572007-04-14 00:12:57 +00001613
Nick Lewycky28a5f252009-09-27 21:33:04 +00001614bool GetElementPtrInst::isInBounds() const {
1615 return cast<GEPOperator>(this)->isInBounds();
1616}
1617
Chandler Carruth1e140532012-12-11 10:29:10 +00001618bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1619 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001620 // Delegate to the generic GEPOperator implementation.
1621 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001622}
1623
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001624//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001625// ExtractElementInst Implementation
1626//===----------------------------------------------------------------------===//
1627
1628ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001629 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001630 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001631 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001632 ExtractElement,
1633 OperandTraits<ExtractElementInst>::op_begin(this),
1634 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001635 assert(isValidOperands(Val, Index) &&
1636 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001637 Op<0>() = Val;
1638 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001639 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001640}
1641
1642ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001643 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001644 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001645 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001646 ExtractElement,
1647 OperandTraits<ExtractElementInst>::op_begin(this),
1648 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001649 assert(isValidOperands(Val, Index) &&
1650 "Invalid extractelement instruction operands!");
1651
Gabor Greif2d3024d2008-05-26 21:33:52 +00001652 Op<0>() = Val;
1653 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001654 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001655}
1656
Chris Lattner65511ff2006-10-05 06:24:58 +00001657
Chris Lattner54865b32006-04-08 04:05:48 +00001658bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001659 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001660 return false;
1661 return true;
1662}
1663
1664
Robert Bocchino23004482006-01-10 19:05:34 +00001665//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001666// InsertElementInst Implementation
1667//===----------------------------------------------------------------------===//
1668
Chris Lattner54865b32006-04-08 04:05:48 +00001669InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001670 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001671 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001672 : Instruction(Vec->getType(), InsertElement,
1673 OperandTraits<InsertElementInst>::op_begin(this),
1674 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001675 assert(isValidOperands(Vec, Elt, Index) &&
1676 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001677 Op<0>() = Vec;
1678 Op<1>() = Elt;
1679 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001680 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001681}
1682
Chris Lattner54865b32006-04-08 04:05:48 +00001683InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001684 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001685 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001686 : Instruction(Vec->getType(), InsertElement,
1687 OperandTraits<InsertElementInst>::op_begin(this),
1688 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001689 assert(isValidOperands(Vec, Elt, Index) &&
1690 "Invalid insertelement instruction operands!");
1691
Gabor Greif2d3024d2008-05-26 21:33:52 +00001692 Op<0>() = Vec;
1693 Op<1>() = Elt;
1694 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001695 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001696}
1697
Chris Lattner54865b32006-04-08 04:05:48 +00001698bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1699 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001700 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001701 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001702
Reid Spencerd84d35b2007-02-15 02:26:10 +00001703 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001704 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001705
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001706 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001707 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001708 return true;
1709}
1710
1711
Robert Bocchinoca27f032006-01-17 20:07:22 +00001712//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001713// ShuffleVectorInst Implementation
1714//===----------------------------------------------------------------------===//
1715
1716ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001717 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001718 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001719: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001720 cast<VectorType>(Mask->getType())->getNumElements()),
1721 ShuffleVector,
1722 OperandTraits<ShuffleVectorInst>::op_begin(this),
1723 OperandTraits<ShuffleVectorInst>::operands(this),
1724 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001725 assert(isValidOperands(V1, V2, Mask) &&
1726 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001727 Op<0>() = V1;
1728 Op<1>() = V2;
1729 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001730 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001731}
1732
1733ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001734 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001735 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001736: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1737 cast<VectorType>(Mask->getType())->getNumElements()),
1738 ShuffleVector,
1739 OperandTraits<ShuffleVectorInst>::op_begin(this),
1740 OperandTraits<ShuffleVectorInst>::operands(this),
1741 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001742 assert(isValidOperands(V1, V2, Mask) &&
1743 "Invalid shuffle vector instruction operands!");
1744
Gabor Greif2d3024d2008-05-26 21:33:52 +00001745 Op<0>() = V1;
1746 Op<1>() = V2;
1747 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001748 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001749}
1750
Mon P Wang25f01062008-11-10 04:46:22 +00001751bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001752 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001753 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001754 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001755 return false;
1756
Chris Lattner1dcb6542012-01-25 23:49:49 +00001757 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001758 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001759 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001760 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001761
1762 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001763 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1764 return true;
1765
Nate Begeman60a31c32010-08-13 00:16:46 +00001766 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001767 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001768 for (Value *Op : MV->operands()) {
1769 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001770 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001771 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001772 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001773 return false;
1774 }
1775 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001776 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001777 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001778
1779 if (const ConstantDataSequential *CDS =
1780 dyn_cast<ConstantDataSequential>(Mask)) {
1781 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1782 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1783 if (CDS->getElementAsInteger(i) >= V1Size*2)
1784 return false;
1785 return true;
1786 }
1787
1788 // The bitcode reader can create a place holder for a forward reference
1789 // used as the shuffle mask. When this occurs, the shuffle mask will
1790 // fall into this case and fail. To avoid this error, do this bit of
1791 // ugliness to allow such a mask pass.
1792 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1793 if (CE->getOpcode() == Instruction::UserOp1)
1794 return true;
1795
1796 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001797}
1798
Chris Lattnerf724e342008-03-02 05:28:33 +00001799/// getMaskValue - Return the index from the shuffle mask for the specified
1800/// output result. This is either -1 if the element is undef or a number less
1801/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001802int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1803 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1804 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001805 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001806 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001807 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001808 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001809 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001810}
1811
Chris Lattner1dcb6542012-01-25 23:49:49 +00001812/// getShuffleMask - Return the full mask for this instruction, where each
1813/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001814void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1815 SmallVectorImpl<int> &Result) {
1816 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001817
Chris Lattnercf129702012-01-26 02:51:13 +00001818 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001819 for (unsigned i = 0; i != NumElts; ++i)
1820 Result.push_back(CDS->getElementAsInteger(i));
1821 return;
1822 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001823 for (unsigned i = 0; i != NumElts; ++i) {
1824 Constant *C = Mask->getAggregateElement(i);
1825 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001826 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001827 }
1828}
1829
1830
Dan Gohman12fce772008-05-15 19:50:34 +00001831//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001832// InsertValueInst Class
1833//===----------------------------------------------------------------------===//
1834
Jay Foad57aa6362011-07-13 10:26:04 +00001835void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1836 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001837 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001838
1839 // There's no fundamental reason why we require at least one index
1840 // (other than weirdness with &*IdxBegin being invalid; see
1841 // getelementptr's init routine for example). But there's no
1842 // present need to support it.
1843 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1844
1845 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001846 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001847 Op<0>() = Agg;
1848 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001849
Jay Foad57aa6362011-07-13 10:26:04 +00001850 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001851 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001852}
1853
1854InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001855 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001856 OperandTraits<InsertValueInst>::op_begin(this), 2),
1857 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001858 Op<0>() = IVI.getOperand(0);
1859 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001860 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001861}
1862
1863//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001864// ExtractValueInst Class
1865//===----------------------------------------------------------------------===//
1866
Jay Foad57aa6362011-07-13 10:26:04 +00001867void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001868 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001869
Jay Foad57aa6362011-07-13 10:26:04 +00001870 // There's no fundamental reason why we require at least one index.
1871 // But there's no present need to support it.
1872 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001873
Jay Foad57aa6362011-07-13 10:26:04 +00001874 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001875 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001876}
1877
1878ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001879 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001880 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001881 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001882}
1883
Dan Gohman12fce772008-05-15 19:50:34 +00001884// getIndexedType - Returns the type of the element that would be extracted
1885// with an extractvalue instruction with the specified parameters.
1886//
1887// A null type is returned if the indices are invalid for the specified
1888// pointer type.
1889//
Chris Lattner229907c2011-07-18 04:54:35 +00001890Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001891 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001892 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001893 // We can't use CompositeType::indexValid(Index) here.
1894 // indexValid() always returns true for arrays because getelementptr allows
1895 // out-of-bounds indices. Since we don't allow those for extractvalue and
1896 // insertvalue we need to check array indexing manually.
1897 // Since the only other types we can index into are struct types it's just
1898 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001899 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001900 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001901 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001902 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001903 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001904 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001905 } else {
1906 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001907 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001908 }
1909
1910 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001911 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001912 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001913}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001914
1915//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001916// BinaryOperator Class
1917//===----------------------------------------------------------------------===//
1918
Chris Lattner2195fc42007-02-24 00:55:48 +00001919BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001920 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001921 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001922 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001923 OperandTraits<BinaryOperator>::op_begin(this),
1924 OperandTraits<BinaryOperator>::operands(this),
1925 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001926 Op<0>() = S1;
1927 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001928 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001929 setName(Name);
1930}
1931
1932BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001933 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001934 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001935 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001936 OperandTraits<BinaryOperator>::op_begin(this),
1937 OperandTraits<BinaryOperator>::operands(this),
1938 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001939 Op<0>() = S1;
1940 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001941 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001942 setName(Name);
1943}
1944
1945
1946void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001947 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001948 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001949 assert(LHS->getType() == RHS->getType() &&
1950 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001951#ifndef NDEBUG
1952 switch (iType) {
1953 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001954 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001955 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001956 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001957 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001958 "Tried to create an integer operation on a non-integer type!");
1959 break;
1960 case FAdd: case FSub:
1961 case FMul:
1962 assert(getType() == LHS->getType() &&
1963 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001964 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001965 "Tried to create a floating-point operation on a "
1966 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001967 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001968 case UDiv:
1969 case SDiv:
1970 assert(getType() == LHS->getType() &&
1971 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001972 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001973 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001974 "Incorrect operand type (not integer) for S/UDIV");
1975 break;
1976 case FDiv:
1977 assert(getType() == LHS->getType() &&
1978 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001979 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001980 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001981 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001982 case URem:
1983 case SRem:
1984 assert(getType() == LHS->getType() &&
1985 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001986 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001987 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001988 "Incorrect operand type (not integer) for S/UREM");
1989 break;
1990 case FRem:
1991 assert(getType() == LHS->getType() &&
1992 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001993 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001994 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001995 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001996 case Shl:
1997 case LShr:
1998 case AShr:
1999 assert(getType() == LHS->getType() &&
2000 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002001 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002002 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002003 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002004 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002005 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002006 case And: case Or:
2007 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002008 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002009 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002010 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002011 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002012 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002013 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002014 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002015 default:
2016 break;
2017 }
2018#endif
2019}
2020
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002021BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002022 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002023 Instruction *InsertBefore) {
2024 assert(S1->getType() == S2->getType() &&
2025 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002026 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002027}
2028
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002029BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002030 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002031 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002032 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002033 InsertAtEnd->getInstList().push_back(Res);
2034 return Res;
2035}
2036
Dan Gohman5476cfd2009-08-12 16:23:25 +00002037BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002038 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002039 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002040 return new BinaryOperator(Instruction::Sub,
2041 zero, Op,
2042 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002043}
2044
Dan Gohman5476cfd2009-08-12 16:23:25 +00002045BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002046 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002047 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002048 return new BinaryOperator(Instruction::Sub,
2049 zero, Op,
2050 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002051}
2052
Dan Gohman4ab44202009-12-18 02:58:50 +00002053BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2054 Instruction *InsertBefore) {
2055 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2056 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2057}
2058
2059BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2060 BasicBlock *InsertAtEnd) {
2061 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2062 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2063}
2064
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002065BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2066 Instruction *InsertBefore) {
2067 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2068 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2069}
2070
2071BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2072 BasicBlock *InsertAtEnd) {
2073 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2074 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2075}
2076
Dan Gohman5476cfd2009-08-12 16:23:25 +00002077BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002078 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002079 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002080 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002081 Op->getType(), Name, InsertBefore);
2082}
2083
Dan Gohman5476cfd2009-08-12 16:23:25 +00002084BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002085 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002086 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002087 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002088 Op->getType(), Name, InsertAtEnd);
2089}
2090
Dan Gohman5476cfd2009-08-12 16:23:25 +00002091BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002092 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002093 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002094 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002095 Op->getType(), Name, InsertBefore);
2096}
2097
Dan Gohman5476cfd2009-08-12 16:23:25 +00002098BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002099 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002100 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002101 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002102 Op->getType(), Name, InsertAtEnd);
2103}
2104
2105
2106// isConstantAllOnes - Helper function for several functions below
2107static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002108 if (const Constant *C = dyn_cast<Constant>(V))
2109 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002110 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002111}
2112
Owen Andersonbb2501b2009-07-13 22:18:28 +00002113bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002114 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2115 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002116 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2117 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002118 return false;
2119}
2120
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002121bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002122 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2123 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002124 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2125 if (!IgnoreZeroSign)
2126 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2127 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2128 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002129 return false;
2130}
2131
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002132bool BinaryOperator::isNot(const Value *V) {
2133 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2134 return (Bop->getOpcode() == Instruction::Xor &&
2135 (isConstantAllOnes(Bop->getOperand(1)) ||
2136 isConstantAllOnes(Bop->getOperand(0))));
2137 return false;
2138}
2139
Chris Lattner2c7d1772005-04-24 07:28:37 +00002140Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002141 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002142}
2143
Chris Lattner2c7d1772005-04-24 07:28:37 +00002144const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2145 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002146}
2147
Dan Gohmana5b96452009-06-04 22:49:04 +00002148Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002149 return cast<BinaryOperator>(BinOp)->getOperand(1);
2150}
2151
2152const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2153 return getFNegArgument(const_cast<Value*>(BinOp));
2154}
2155
Chris Lattner2c7d1772005-04-24 07:28:37 +00002156Value *BinaryOperator::getNotArgument(Value *BinOp) {
2157 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2158 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2159 Value *Op0 = BO->getOperand(0);
2160 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002161 if (isConstantAllOnes(Op0)) return Op1;
2162
2163 assert(isConstantAllOnes(Op1));
2164 return Op0;
2165}
2166
Chris Lattner2c7d1772005-04-24 07:28:37 +00002167const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2168 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002169}
2170
2171
2172// swapOperands - Exchange the two operands to this instruction. This
2173// instruction is safe to use on any binary instruction and does not
2174// modify the semantics of the instruction. If the instruction is
2175// order dependent (SetLT f.e.) the opcode is changed.
2176//
2177bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002178 if (!isCommutative())
2179 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002180 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002181 return false;
2182}
2183
Dan Gohman1b849082009-09-07 23:54:19 +00002184void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2185 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2186}
2187
2188void BinaryOperator::setHasNoSignedWrap(bool b) {
2189 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2190}
2191
2192void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002193 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002194}
2195
Nick Lewycky28a5f252009-09-27 21:33:04 +00002196bool BinaryOperator::hasNoUnsignedWrap() const {
2197 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2198}
2199
2200bool BinaryOperator::hasNoSignedWrap() const {
2201 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2202}
2203
2204bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002205 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002206}
2207
Sanjay Patela982d992014-09-03 01:06:50 +00002208void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002209 // Copy the wrapping flags.
2210 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2211 setHasNoSignedWrap(OB->hasNoSignedWrap());
2212 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2213 }
2214
2215 // Copy the exact flag.
2216 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2217 setIsExact(PE->isExact());
2218
2219 // Copy the fast-math flags.
2220 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002221 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002222}
2223
Sanjay Patela982d992014-09-03 01:06:50 +00002224void BinaryOperator::andIRFlags(const Value *V) {
2225 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2226 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2227 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2228 }
2229
2230 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2231 setIsExact(isExact() & PE->isExact());
2232
2233 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2234 FastMathFlags FM = getFastMathFlags();
2235 FM &= FP->getFastMathFlags();
2236 copyFastMathFlags(FM);
2237 }
2238}
2239
2240
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002241//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002242// FPMathOperator Class
2243//===----------------------------------------------------------------------===//
2244
2245/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2246/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002247/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002248float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002249 const MDNode *MD =
2250 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002251 if (!MD)
2252 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002253 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002254 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002255}
2256
2257
2258//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002259// CastInst Class
2260//===----------------------------------------------------------------------===//
2261
David Blaikie3a15e142011-12-01 08:00:17 +00002262void CastInst::anchor() {}
2263
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264// Just determine if this cast only deals with integral->integral conversion.
2265bool CastInst::isIntegerCast() const {
2266 switch (getOpcode()) {
2267 default: return false;
2268 case Instruction::ZExt:
2269 case Instruction::SExt:
2270 case Instruction::Trunc:
2271 return true;
2272 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002273 return getOperand(0)->getType()->isIntegerTy() &&
2274 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002276}
2277
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002278bool CastInst::isLosslessCast() const {
2279 // Only BitCast can be lossless, exit fast if we're not BitCast
2280 if (getOpcode() != Instruction::BitCast)
2281 return false;
2282
2283 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002284 Type* SrcTy = getOperand(0)->getType();
2285 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002286 if (SrcTy == DstTy)
2287 return true;
2288
Reid Spencer8d9336d2006-12-31 05:26:44 +00002289 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002290 if (SrcTy->isPointerTy())
2291 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002292 return false; // Other types have no identity values
2293}
2294
2295/// This function determines if the CastInst does not require any bits to be
2296/// changed in order to effect the cast. Essentially, it identifies cases where
2297/// no code gen is necessary for the cast, hence the name no-op cast. For
2298/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002299/// # bitcast i32* %x to i8*
2300/// # bitcast <2 x i32> %x to <4 x i16>
2301/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002302/// @brief Determine if the described cast is a no-op.
2303bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002304 Type *SrcTy,
2305 Type *DestTy,
2306 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002307 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002308 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309 case Instruction::Trunc:
2310 case Instruction::ZExt:
2311 case Instruction::SExt:
2312 case Instruction::FPTrunc:
2313 case Instruction::FPExt:
2314 case Instruction::UIToFP:
2315 case Instruction::SIToFP:
2316 case Instruction::FPToUI:
2317 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002318 case Instruction::AddrSpaceCast:
2319 // TODO: Target informations may give a more accurate answer here.
2320 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002321 case Instruction::BitCast:
2322 return true; // BitCast never modifies bits.
2323 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002324 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002325 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002327 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002328 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 }
2330}
2331
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002332/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002333bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002334 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2335}
2336
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002337bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002338 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002339 if (getOpcode() == Instruction::PtrToInt)
2340 PtrOpTy = getOperand(0)->getType();
2341 else if (getOpcode() == Instruction::IntToPtr)
2342 PtrOpTy = getType();
2343
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002344 Type *IntPtrTy =
2345 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002346
2347 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2348}
2349
2350/// This function determines if a pair of casts can be eliminated and what
2351/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352/// instructions like this:
2353/// * %F = firstOpcode SrcTy %x to MidTy
2354/// * %S = secondOpcode MidTy %F to DstTy
2355/// The function returns a resultOpcode so these two casts can be replaced with:
2356/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002357/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358unsigned CastInst::isEliminableCastPair(
2359 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002360 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2361 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362 // Define the 144 possibilities for these two cast instructions. The values
2363 // in this matrix determine what to do in a given situation and select the
2364 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002365 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366 // the following cast properties:
2367 //
2368 // Size Compare Source Destination
2369 // Operator Src ? Size Type Sign Type Sign
2370 // -------- ------------ ------------------- ---------------------
2371 // TRUNC > Integer Any Integral Any
2372 // ZEXT < Integral Unsigned Integer Any
2373 // SEXT < Integral Signed Integer Any
2374 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002375 // FPTOSI n/a FloatPt n/a Integral Signed
2376 // UITOFP n/a Integral Unsigned FloatPt n/a
2377 // SITOFP n/a Integral Signed FloatPt n/a
2378 // FPTRUNC > FloatPt n/a FloatPt n/a
2379 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380 // PTRTOINT n/a Pointer n/a Integral Unsigned
2381 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002382 // BITCAST = FirstClass n/a FirstClass n/a
2383 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002384 //
2385 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002386 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2387 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002388 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002389 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002390 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002391 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002392 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2394 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002395 // T F F U S F F P I B A -+
2396 // R Z S P P I I T P 2 N T S |
2397 // U E E 2 2 2 2 R E I T C C +- secondOp
2398 // N X X U S F F N X N 2 V V |
2399 // C T T I I P P C T T P T T -+
2400 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002401 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002402 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2403 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2404 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2405 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2406 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002407 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002408 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2409 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2410 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2411 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2412 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002414
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002415 // TODO: This logic could be encoded into the table above and handled in the
2416 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002417 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002418 // merging. However, any pair of bitcasts are allowed.
2419 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2420 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2421 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002422
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002423 // Check if any of the casts convert scalars <-> vectors.
2424 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2425 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2426 if (!AreBothBitcasts)
2427 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428
2429 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2430 [secondOp-Instruction::CastOpsBegin];
2431 switch (ElimCase) {
2432 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002433 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 return 0;
2435 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002436 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437 return firstOp;
2438 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002439 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440 return secondOp;
2441 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002442 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002443 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002444 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002445 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446 return firstOp;
2447 return 0;
2448 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002449 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002450 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002451 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452 return firstOp;
2453 return 0;
2454 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002455 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002456 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002457 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458 return secondOp;
2459 return 0;
2460 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002461 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002462 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002463 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 return secondOp;
2465 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002466 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002467 // Cannot simplify if address spaces are different!
2468 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2469 return 0;
2470
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002471 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002472 // We can still fold this without knowing the actual sizes as long we
2473 // know that the intermediate pointer is the largest possible
2474 // pointer size.
2475 // FIXME: Is this always true?
2476 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002477 return Instruction::BitCast;
2478
2479 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002480 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002481 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002482 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483 if (MidSize >= PtrSize)
2484 return Instruction::BitCast;
2485 return 0;
2486 }
2487 case 8: {
2488 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2489 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2490 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002491 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2492 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493 if (SrcSize == DstSize)
2494 return Instruction::BitCast;
2495 else if (SrcSize < DstSize)
2496 return firstOp;
2497 return secondOp;
2498 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002499 case 9:
2500 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501 return Instruction::ZExt;
2502 case 10:
2503 // fpext followed by ftrunc is allowed if the bit size returned to is
2504 // the same as the original, in which case its just a bitcast
2505 if (SrcTy == DstTy)
2506 return Instruction::BitCast;
2507 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002508 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002510 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002511 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002512 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002513 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2514 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515 if (SrcSize <= PtrSize && SrcSize == DstSize)
2516 return Instruction::BitCast;
2517 return 0;
2518 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002519 case 12: {
2520 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2521 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2522 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2523 return Instruction::AddrSpaceCast;
2524 return Instruction::BitCast;
2525 }
2526 case 13:
2527 // FIXME: this state can be merged with (1), but the following assert
2528 // is useful to check the correcteness of the sequence due to semantic
2529 // change of bitcast.
2530 assert(
2531 SrcTy->isPtrOrPtrVectorTy() &&
2532 MidTy->isPtrOrPtrVectorTy() &&
2533 DstTy->isPtrOrPtrVectorTy() &&
2534 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2535 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2536 "Illegal addrspacecast, bitcast sequence!");
2537 // Allowed, use first cast's opcode
2538 return firstOp;
2539 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002540 // bitcast, addrspacecast -> addrspacecast if the element type of
2541 // bitcast's source is the same as that of addrspacecast's destination.
2542 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2543 return Instruction::AddrSpaceCast;
2544 return 0;
2545
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002546 case 15:
2547 // FIXME: this state can be merged with (1), but the following assert
2548 // is useful to check the correcteness of the sequence due to semantic
2549 // change of bitcast.
2550 assert(
2551 SrcTy->isIntOrIntVectorTy() &&
2552 MidTy->isPtrOrPtrVectorTy() &&
2553 DstTy->isPtrOrPtrVectorTy() &&
2554 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2555 "Illegal inttoptr, bitcast sequence!");
2556 // Allowed, use first cast's opcode
2557 return firstOp;
2558 case 16:
2559 // FIXME: this state can be merged with (2), but the following assert
2560 // is useful to check the correcteness of the sequence due to semantic
2561 // change of bitcast.
2562 assert(
2563 SrcTy->isPtrOrPtrVectorTy() &&
2564 MidTy->isPtrOrPtrVectorTy() &&
2565 DstTy->isIntOrIntVectorTy() &&
2566 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2567 "Illegal bitcast, ptrtoint sequence!");
2568 // Allowed, use second cast's opcode
2569 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002570 case 17:
2571 // (sitofp (zext x)) -> (uitofp x)
2572 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002574 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002576 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577 default:
Craig Topperc514b542012-02-05 22:14:15 +00002578 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580}
2581
Chris Lattner229907c2011-07-18 04:54:35 +00002582CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002583 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002584 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585 // Construct and return the appropriate CastInst subclass
2586 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002587 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2588 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2589 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2590 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2591 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2592 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2593 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2594 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2595 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2596 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2597 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2598 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2599 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2600 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602}
2603
Chris Lattner229907c2011-07-18 04:54:35 +00002604CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002605 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002606 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607 // Construct and return the appropriate CastInst subclass
2608 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002609 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2610 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2611 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2612 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2613 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2614 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2615 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2616 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2617 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2618 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2619 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2620 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2621 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2622 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624}
2625
Chris Lattner229907c2011-07-18 04:54:35 +00002626CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002627 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002628 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002629 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002630 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2631 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002632}
2633
Chris Lattner229907c2011-07-18 04:54:35 +00002634CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002635 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002636 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002637 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002638 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2639 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002640}
2641
Chris Lattner229907c2011-07-18 04:54:35 +00002642CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002643 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002644 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002645 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002646 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2647 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002648}
2649
Chris Lattner229907c2011-07-18 04:54:35 +00002650CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002651 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002652 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002653 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002654 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2655 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002656}
2657
Chris Lattner229907c2011-07-18 04:54:35 +00002658CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002659 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002660 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002661 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002662 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2663 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002664}
2665
Chris Lattner229907c2011-07-18 04:54:35 +00002666CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002667 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002668 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002669 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002670 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2671 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002672}
2673
Chris Lattner229907c2011-07-18 04:54:35 +00002674CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002675 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002676 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002677 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2678 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2679 "Invalid cast");
2680 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002681 assert((!Ty->isVectorTy() ||
2682 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002683 "Invalid cast");
2684
Matt Arsenault065ced92013-07-31 00:17:33 +00002685 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002686 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002687
Matt Arsenault740980e2014-07-14 17:24:35 +00002688 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002689}
2690
2691/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002692CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2693 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002694 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002695 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2696 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002697 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002698 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002699 assert((!Ty->isVectorTy() ||
2700 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002701 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002702
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002703 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002704 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002705
Matt Arsenault740980e2014-07-14 17:24:35 +00002706 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2707}
2708
2709CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2710 Value *S, Type *Ty,
2711 const Twine &Name,
2712 BasicBlock *InsertAtEnd) {
2713 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2714 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2715
2716 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2717 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2718
2719 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2720}
2721
2722CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2723 Value *S, Type *Ty,
2724 const Twine &Name,
2725 Instruction *InsertBefore) {
2726 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2727 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2728
2729 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002730 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2731
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002732 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002733}
2734
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002735CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2736 const Twine &Name,
2737 Instruction *InsertBefore) {
2738 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2739 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2740 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2741 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2742
2743 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2744}
2745
Matt Arsenault740980e2014-07-14 17:24:35 +00002746CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002747 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002748 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002749 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002750 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002751 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2752 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002753 Instruction::CastOps opcode =
2754 (SrcBits == DstBits ? Instruction::BitCast :
2755 (SrcBits > DstBits ? Instruction::Trunc :
2756 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002757 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002758}
2759
Chris Lattner229907c2011-07-18 04:54:35 +00002760CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002761 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002762 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002763 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002764 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002765 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2766 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002767 Instruction::CastOps opcode =
2768 (SrcBits == DstBits ? Instruction::BitCast :
2769 (SrcBits > DstBits ? Instruction::Trunc :
2770 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002771 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002772}
2773
Chris Lattner229907c2011-07-18 04:54:35 +00002774CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002775 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002776 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002777 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002778 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002779 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2780 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002781 Instruction::CastOps opcode =
2782 (SrcBits == DstBits ? Instruction::BitCast :
2783 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002784 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002785}
2786
Chris Lattner229907c2011-07-18 04:54:35 +00002787CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002788 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002789 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002790 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002791 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002792 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2793 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002794 Instruction::CastOps opcode =
2795 (SrcBits == DstBits ? Instruction::BitCast :
2796 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002797 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002798}
2799
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002800// Check whether it is valid to call getCastOpcode for these types.
2801// This routine must be kept in sync with getCastOpcode.
2802bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2803 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2804 return false;
2805
2806 if (SrcTy == DestTy)
2807 return true;
2808
2809 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2810 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2811 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2812 // An element by element cast. Valid if casting the elements is valid.
2813 SrcTy = SrcVecTy->getElementType();
2814 DestTy = DestVecTy->getElementType();
2815 }
2816
2817 // Get the bit sizes, we'll need these
2818 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2819 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2820
2821 // Run through the possibilities ...
2822 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002823 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002824 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002825 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002826 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002827 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002828 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002829 // Casting from something else
2830 return SrcTy->isPointerTy();
2831 }
2832 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2833 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002834 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002835 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002836 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002837 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002838 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002839 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002840 return false;
2841 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002842 if (DestTy->isVectorTy()) // Casting to vector
2843 return DestBits == SrcBits;
2844 if (DestTy->isPointerTy()) { // Casting to pointer
2845 if (SrcTy->isPointerTy()) // Casting from pointer
2846 return true;
2847 return SrcTy->isIntegerTy(); // Casting from integral
2848 }
2849 if (DestTy->isX86_MMXTy()) {
2850 if (SrcTy->isVectorTy())
2851 return DestBits == SrcBits; // 64-bit vector to MMX
2852 return false;
2853 } // Casting to something else
2854 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002855}
2856
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002857bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2858 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2859 return false;
2860
2861 if (SrcTy == DestTy)
2862 return true;
2863
2864 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2865 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2866 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2867 // An element by element cast. Valid if casting the elements is valid.
2868 SrcTy = SrcVecTy->getElementType();
2869 DestTy = DestVecTy->getElementType();
2870 }
2871 }
2872 }
2873
2874 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2875 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2876 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2877 }
2878 }
2879
2880 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2881 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2882
2883 // Could still have vectors of pointers if the number of elements doesn't
2884 // match
2885 if (SrcBits == 0 || DestBits == 0)
2886 return false;
2887
2888 if (SrcBits != DestBits)
2889 return false;
2890
2891 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2892 return false;
2893
2894 return true;
2895}
2896
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002897bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002898 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002899 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2900 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002901 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002902 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2903 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002904 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002905
2906 return isBitCastable(SrcTy, DestTy);
2907}
2908
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002909// Provide a way to get a "cast" where the cast opcode is inferred from the
2910// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002911// logic in the castIsValid function below. This axiom should hold:
2912// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2913// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002914// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002915// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002916Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002917CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002918 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2919 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002920
Duncan Sands55e50902008-01-06 10:12:28 +00002921 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2922 "Only first class types are castable!");
2923
Duncan Sandsa8514532011-05-18 07:13:41 +00002924 if (SrcTy == DestTy)
2925 return BitCast;
2926
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002927 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002928 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2929 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002930 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2931 // An element by element cast. Find the appropriate opcode based on the
2932 // element types.
2933 SrcTy = SrcVecTy->getElementType();
2934 DestTy = DestVecTy->getElementType();
2935 }
2936
2937 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002938 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2939 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002940
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002942 if (DestTy->isIntegerTy()) { // Casting to integral
2943 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002944 if (DestBits < SrcBits)
2945 return Trunc; // int -> smaller int
2946 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002947 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002948 return SExt; // signed -> SEXT
2949 else
2950 return ZExt; // unsigned -> ZEXT
2951 } else {
2952 return BitCast; // Same size, No-op cast
2953 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002954 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002955 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002956 return FPToSI; // FP -> sint
2957 else
2958 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002959 } else if (SrcTy->isVectorTy()) {
2960 assert(DestBits == SrcBits &&
2961 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002962 return BitCast; // Same size, no-op cast
2963 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002964 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002965 "Casting from a value that is not first-class type");
2966 return PtrToInt; // ptr -> int
2967 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002968 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2969 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002970 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002971 return SIToFP; // sint -> FP
2972 else
2973 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002974 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002975 if (DestBits < SrcBits) {
2976 return FPTrunc; // FP -> smaller FP
2977 } else if (DestBits > SrcBits) {
2978 return FPExt; // FP -> larger FP
2979 } else {
2980 return BitCast; // same size, no-op cast
2981 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002982 } else if (SrcTy->isVectorTy()) {
2983 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002984 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002985 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002986 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002987 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002988 } else if (DestTy->isVectorTy()) {
2989 assert(DestBits == SrcBits &&
2990 "Illegal cast to vector (wrong type or size)");
2991 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002992 } else if (DestTy->isPointerTy()) {
2993 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002994 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2995 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002996 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002997 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002998 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002999 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003000 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003001 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003002 if (SrcTy->isVectorTy()) {
3003 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003004 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003005 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003006 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003007 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003008 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003009}
3010
3011//===----------------------------------------------------------------------===//
3012// CastInst SubClass Constructors
3013//===----------------------------------------------------------------------===//
3014
3015/// Check that the construction parameters for a CastInst are correct. This
3016/// could be broken out into the separate constructors but it is useful to have
3017/// it in one place and to eliminate the redundant code for getting the sizes
3018/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003019bool
Chris Lattner229907c2011-07-18 04:54:35 +00003020CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003021
3022 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003023 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003024
Chris Lattner37bc78a2010-01-26 21:51:43 +00003025 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3026 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003027 return false;
3028
3029 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003030 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3031 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003032
Duncan Sands7f646562011-05-18 09:21:57 +00003033 // If these are vector types, get the lengths of the vectors (using zero for
3034 // scalar types means that checking that vector lengths match also checks that
3035 // scalars are not being converted to vectors or vectors to scalars).
3036 unsigned SrcLength = SrcTy->isVectorTy() ?
3037 cast<VectorType>(SrcTy)->getNumElements() : 0;
3038 unsigned DstLength = DstTy->isVectorTy() ?
3039 cast<VectorType>(DstTy)->getNumElements() : 0;
3040
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003041 // Switch on the opcode provided
3042 switch (op) {
3043 default: return false; // This is an input error
3044 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003045 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3046 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003047 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003048 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3049 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003050 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003051 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3052 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003053 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003054 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3055 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003056 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003057 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3058 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003059 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003060 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003061 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3062 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003063 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003064 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003065 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3066 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003067 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003068 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003069 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003070 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3071 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3072 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003073 return SrcTy->getScalarType()->isPointerTy() &&
3074 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003075 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003076 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003077 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003078 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3079 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3080 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003081 return SrcTy->getScalarType()->isIntegerTy() &&
3082 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003083 case Instruction::BitCast: {
3084 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3085 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3086
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003087 // BitCast implies a no-op cast of type only. No bits change.
3088 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003089 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003090 return false;
3091
Alp Tokerf907b892013-12-05 05:44:44 +00003092 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003093 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003094 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003095 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3096
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003097 // If both are pointers then the address spaces must match.
3098 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3099 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003100
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003101 // A vector of pointers must have the same number of elements.
3102 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3103 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3104 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3105
3106 return false;
3107 }
3108
3109 return true;
3110 }
3111 case Instruction::AddrSpaceCast: {
3112 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3113 if (!SrcPtrTy)
3114 return false;
3115
3116 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3117 if (!DstPtrTy)
3118 return false;
3119
3120 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3121 return false;
3122
3123 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3124 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3125 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3126
3127 return false;
3128 }
3129
3130 return true;
3131 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003132 }
3133}
3134
3135TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003136 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003137) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003138 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003139}
3140
3141TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003142 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003144 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003145}
3146
3147ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003148 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003149) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003150 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003151}
3152
3153ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003154 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003155) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003156 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003157}
3158SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003159 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003160) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003161 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003162}
3163
Jeff Cohencc08c832006-12-02 02:22:01 +00003164SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003165 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003166) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003167 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168}
3169
3170FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003171 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003172) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003173 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003174}
3175
3176FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003177 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003178) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003179 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003180}
3181
3182FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003183 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003184) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003185 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186}
3187
3188FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003189 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003190) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003191 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003192}
3193
3194UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003195 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003196) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003197 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003198}
3199
3200UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003201 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003202) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003203 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003204}
3205
3206SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003207 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003208) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003209 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003210}
3211
3212SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003213 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003214) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003215 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003216}
3217
3218FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003219 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003220) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003221 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003222}
3223
3224FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003225 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003226) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003227 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228}
3229
3230FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003231 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003232) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003233 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234}
3235
3236FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003237 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003238) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003239 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240}
3241
3242PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003243 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003244) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003245 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003246}
3247
3248PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003249 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003250) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003251 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003252}
3253
3254IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003255 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003256) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003257 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258}
3259
3260IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003261 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003262) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003263 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264}
3265
3266BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003267 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003268) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003269 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270}
3271
3272BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003273 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003274) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003275 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003277
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003278AddrSpaceCastInst::AddrSpaceCastInst(
3279 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3280) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3281 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3282}
3283
3284AddrSpaceCastInst::AddrSpaceCastInst(
3285 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3286) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3287 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3288}
3289
Chris Lattnerf16dc002006-09-17 19:29:56 +00003290//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003291// CmpInst Classes
3292//===----------------------------------------------------------------------===//
3293
Craig Topper3186c012012-09-23 02:12:10 +00003294void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003295
Craig Topper1c3f2832015-12-15 06:11:33 +00003296CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3297 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003298 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003299 OperandTraits<CmpInst>::op_begin(this),
3300 OperandTraits<CmpInst>::operands(this),
3301 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003302 Op<0>() = LHS;
3303 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003304 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003305 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003306}
Gabor Greiff6caff662008-05-10 08:32:32 +00003307
Craig Topper1c3f2832015-12-15 06:11:33 +00003308CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3309 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003310 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003311 OperandTraits<CmpInst>::op_begin(this),
3312 OperandTraits<CmpInst>::operands(this),
3313 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003314 Op<0>() = LHS;
3315 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003316 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003317 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003318}
3319
3320CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003321CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003322 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003323 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003324 if (InsertBefore)
3325 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3326 S1, S2, Name);
3327 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003328 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003329 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003330 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003331
3332 if (InsertBefore)
3333 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3334 S1, S2, Name);
3335 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003336 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003337 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003338}
3339
3340CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003341CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003342 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003343 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003344 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3345 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003346 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003347 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3348 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003349}
3350
3351void CmpInst::swapOperands() {
3352 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3353 IC->swapOperands();
3354 else
3355 cast<FCmpInst>(this)->swapOperands();
3356}
3357
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003358bool CmpInst::isCommutative() const {
3359 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003360 return IC->isCommutative();
3361 return cast<FCmpInst>(this)->isCommutative();
3362}
3363
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003364bool CmpInst::isEquality() const {
3365 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003366 return IC->isEquality();
3367 return cast<FCmpInst>(this)->isEquality();
3368}
3369
3370
Dan Gohman4e724382008-05-31 02:47:54 +00003371CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003372 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003373 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003374 case ICMP_EQ: return ICMP_NE;
3375 case ICMP_NE: return ICMP_EQ;
3376 case ICMP_UGT: return ICMP_ULE;
3377 case ICMP_ULT: return ICMP_UGE;
3378 case ICMP_UGE: return ICMP_ULT;
3379 case ICMP_ULE: return ICMP_UGT;
3380 case ICMP_SGT: return ICMP_SLE;
3381 case ICMP_SLT: return ICMP_SGE;
3382 case ICMP_SGE: return ICMP_SLT;
3383 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003384
Dan Gohman4e724382008-05-31 02:47:54 +00003385 case FCMP_OEQ: return FCMP_UNE;
3386 case FCMP_ONE: return FCMP_UEQ;
3387 case FCMP_OGT: return FCMP_ULE;
3388 case FCMP_OLT: return FCMP_UGE;
3389 case FCMP_OGE: return FCMP_ULT;
3390 case FCMP_OLE: return FCMP_UGT;
3391 case FCMP_UEQ: return FCMP_ONE;
3392 case FCMP_UNE: return FCMP_OEQ;
3393 case FCMP_UGT: return FCMP_OLE;
3394 case FCMP_ULT: return FCMP_OGE;
3395 case FCMP_UGE: return FCMP_OLT;
3396 case FCMP_ULE: return FCMP_OGT;
3397 case FCMP_ORD: return FCMP_UNO;
3398 case FCMP_UNO: return FCMP_ORD;
3399 case FCMP_TRUE: return FCMP_FALSE;
3400 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003401 }
3402}
3403
Pete Cooper1d078692015-12-14 20:29:16 +00003404void ICmpInst::anchor() {}
3405
Reid Spencer266e42b2006-12-23 06:05:41 +00003406ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3407 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003408 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003409 case ICMP_EQ: case ICMP_NE:
3410 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3411 return pred;
3412 case ICMP_UGT: return ICMP_SGT;
3413 case ICMP_ULT: return ICMP_SLT;
3414 case ICMP_UGE: return ICMP_SGE;
3415 case ICMP_ULE: return ICMP_SLE;
3416 }
3417}
3418
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003419ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3420 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003421 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003422 case ICMP_EQ: case ICMP_NE:
3423 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3424 return pred;
3425 case ICMP_SGT: return ICMP_UGT;
3426 case ICMP_SLT: return ICMP_ULT;
3427 case ICMP_SGE: return ICMP_UGE;
3428 case ICMP_SLE: return ICMP_ULE;
3429 }
3430}
3431
Reid Spencer0286bc12007-02-28 22:00:54 +00003432/// Initialize a set of values that all satisfy the condition with C.
3433///
3434ConstantRange
3435ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3436 APInt Lower(C);
3437 APInt Upper(C);
3438 uint32_t BitWidth = C.getBitWidth();
3439 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003440 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003441 case ICmpInst::ICMP_EQ: ++Upper; break;
3442 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003443 case ICmpInst::ICMP_ULT:
3444 Lower = APInt::getMinValue(BitWidth);
3445 // Check for an empty-set condition.
3446 if (Lower == Upper)
3447 return ConstantRange(BitWidth, /*isFullSet=*/false);
3448 break;
3449 case ICmpInst::ICMP_SLT:
3450 Lower = APInt::getSignedMinValue(BitWidth);
3451 // Check for an empty-set condition.
3452 if (Lower == Upper)
3453 return ConstantRange(BitWidth, /*isFullSet=*/false);
3454 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003455 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003456 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003457 // Check for an empty-set condition.
3458 if (Lower == Upper)
3459 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003460 break;
3461 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003462 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003463 // Check for an empty-set condition.
3464 if (Lower == Upper)
3465 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003466 break;
3467 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003468 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003469 // Check for a full-set condition.
3470 if (Lower == Upper)
3471 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003472 break;
3473 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003474 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003475 // Check for a full-set condition.
3476 if (Lower == Upper)
3477 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003478 break;
3479 case ICmpInst::ICMP_UGE:
3480 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003481 // Check for a full-set condition.
3482 if (Lower == Upper)
3483 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003484 break;
3485 case ICmpInst::ICMP_SGE:
3486 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003487 // Check for a full-set condition.
3488 if (Lower == Upper)
3489 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003490 break;
3491 }
3492 return ConstantRange(Lower, Upper);
3493}
3494
Dan Gohman4e724382008-05-31 02:47:54 +00003495CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003496 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003497 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003498 case ICMP_EQ: case ICMP_NE:
3499 return pred;
3500 case ICMP_SGT: return ICMP_SLT;
3501 case ICMP_SLT: return ICMP_SGT;
3502 case ICMP_SGE: return ICMP_SLE;
3503 case ICMP_SLE: return ICMP_SGE;
3504 case ICMP_UGT: return ICMP_ULT;
3505 case ICMP_ULT: return ICMP_UGT;
3506 case ICMP_UGE: return ICMP_ULE;
3507 case ICMP_ULE: return ICMP_UGE;
3508
Reid Spencerd9436b62006-11-20 01:22:35 +00003509 case FCMP_FALSE: case FCMP_TRUE:
3510 case FCMP_OEQ: case FCMP_ONE:
3511 case FCMP_UEQ: case FCMP_UNE:
3512 case FCMP_ORD: case FCMP_UNO:
3513 return pred;
3514 case FCMP_OGT: return FCMP_OLT;
3515 case FCMP_OLT: return FCMP_OGT;
3516 case FCMP_OGE: return FCMP_OLE;
3517 case FCMP_OLE: return FCMP_OGE;
3518 case FCMP_UGT: return FCMP_ULT;
3519 case FCMP_ULT: return FCMP_UGT;
3520 case FCMP_UGE: return FCMP_ULE;
3521 case FCMP_ULE: return FCMP_UGE;
3522 }
3523}
3524
Sanjoy Das6e78b172015-10-22 19:57:34 +00003525CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3526 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3527
3528 switch (pred) {
3529 default:
3530 llvm_unreachable("Unknown predicate!");
3531 case CmpInst::ICMP_ULT:
3532 return CmpInst::ICMP_SLT;
3533 case CmpInst::ICMP_ULE:
3534 return CmpInst::ICMP_SLE;
3535 case CmpInst::ICMP_UGT:
3536 return CmpInst::ICMP_SGT;
3537 case CmpInst::ICMP_UGE:
3538 return CmpInst::ICMP_SGE;
3539 }
3540}
3541
Craig Topper1c3f2832015-12-15 06:11:33 +00003542bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003543 switch (predicate) {
3544 default: return false;
3545 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3546 case ICmpInst::ICMP_UGE: return true;
3547 }
3548}
3549
Craig Topper1c3f2832015-12-15 06:11:33 +00003550bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003551 switch (predicate) {
3552 default: return false;
3553 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3554 case ICmpInst::ICMP_SGE: return true;
3555 }
3556}
3557
Craig Topper1c3f2832015-12-15 06:11:33 +00003558bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003559 switch (predicate) {
3560 default: return false;
3561 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3562 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3563 case FCmpInst::FCMP_ORD: return true;
3564 }
3565}
3566
Craig Topper1c3f2832015-12-15 06:11:33 +00003567bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003568 switch (predicate) {
3569 default: return false;
3570 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3571 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3572 case FCmpInst::FCMP_UNO: return true;
3573 }
3574}
3575
Craig Topper1c3f2832015-12-15 06:11:33 +00003576bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003577 switch(predicate) {
3578 default: return false;
3579 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3580 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3581 }
3582}
3583
Craig Topper1c3f2832015-12-15 06:11:33 +00003584bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003585 switch(predicate) {
3586 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3587 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3588 default: return false;
3589 }
3590}
3591
3592
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003593//===----------------------------------------------------------------------===//
3594// SwitchInst Implementation
3595//===----------------------------------------------------------------------===//
3596
Chris Lattnerbaf00152010-11-17 05:41:46 +00003597void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3598 assert(Value && Default && NumReserved);
3599 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003600 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003601 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003602
Pete Coopereb31b682015-05-21 22:48:54 +00003603 Op<0>() = Value;
3604 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003605}
3606
Chris Lattner2195fc42007-02-24 00:55:48 +00003607/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3608/// switch on and a default destination. The number of additional cases can
3609/// be specified here to make memory allocation more efficient. This
3610/// constructor can also autoinsert before another instruction.
3611SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3612 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003613 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003614 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003615 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003616}
3617
3618/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3619/// switch on and a default destination. The number of additional cases can
3620/// be specified here to make memory allocation more efficient. This
3621/// constructor also autoinserts at the end of the specified BasicBlock.
3622SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3623 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003624 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003625 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003626 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003627}
3628
Misha Brukmanb1c93172005-04-21 23:48:37 +00003629SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003630 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003631 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003632 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003633 Use *OL = getOperandList();
3634 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003635 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003636 OL[i] = InOL[i];
3637 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003638 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003639 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003640}
3641
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003642
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003643/// addCase - Add an entry to the switch instruction...
3644///
Chris Lattner47ac1872005-02-24 05:32:09 +00003645void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003646 unsigned NewCaseIdx = getNumCases();
3647 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003648 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003649 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003650 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003651 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003652 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003653 CaseIt Case(this, NewCaseIdx);
3654 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003655 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003656}
3657
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003658/// removeCase - This method removes the specified case and its successor
3659/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003660void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003661 unsigned idx = i.getCaseIndex();
3662
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003663 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003664
3665 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003666 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003667
Jay Foad14277722011-02-01 09:22:34 +00003668 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003669 if (2 + (idx + 1) * 2 != NumOps) {
3670 OL[2 + idx * 2] = OL[NumOps - 2];
3671 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003672 }
3673
3674 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003675 OL[NumOps-2].set(nullptr);
3676 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003677 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003678}
3679
Jay Foade98f29d2011-04-01 08:00:58 +00003680/// growOperands - grow operands - This grows the operand list in response
3681/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003682///
Jay Foade98f29d2011-04-01 08:00:58 +00003683void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003684 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003685 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003686
3687 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003688 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003689}
3690
3691
3692BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3693 return getSuccessor(idx);
3694}
3695unsigned SwitchInst::getNumSuccessorsV() const {
3696 return getNumSuccessors();
3697}
3698void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3699 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003700}
Chris Lattnerf22be932004-10-15 23:52:53 +00003701
Chris Lattner3ed871f2009-10-27 19:13:16 +00003702//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003703// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003704//===----------------------------------------------------------------------===//
3705
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003706void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003707 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003708 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003709 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003710 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003711 allocHungoffUses(ReservedSpace);
3712
Pete Coopereb31b682015-05-21 22:48:54 +00003713 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003714}
3715
3716
Jay Foade98f29d2011-04-01 08:00:58 +00003717/// growOperands - grow operands - This grows the operand list in response
3718/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003719///
Jay Foade98f29d2011-04-01 08:00:58 +00003720void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003721 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003722 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003723
3724 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003725 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003726}
3727
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003728IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3729 Instruction *InsertBefore)
3730: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003731 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003732 init(Address, NumCases);
3733}
3734
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003735IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3736 BasicBlock *InsertAtEnd)
3737: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003738 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003739 init(Address, NumCases);
3740}
3741
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003742IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003743 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3744 nullptr, IBI.getNumOperands()) {
3745 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003746 Use *OL = getOperandList();
3747 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003748 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3749 OL[i] = InOL[i];
3750 SubclassOptionalData = IBI.SubclassOptionalData;
3751}
3752
Chris Lattner3ed871f2009-10-27 19:13:16 +00003753/// addDestination - Add a destination.
3754///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003755void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003756 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003757 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003758 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003759 // Initialize some new operands.
3760 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003761 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003762 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003763}
3764
3765/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003766/// indirectbr instruction.
3767void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003768 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3769
3770 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003771 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003772
3773 // Replace this value with the last one.
3774 OL[idx+1] = OL[NumOps-1];
3775
3776 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003777 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003778 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003779}
3780
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003781BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003782 return getSuccessor(idx);
3783}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003784unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003785 return getNumSuccessors();
3786}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003787void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003788 setSuccessor(idx, B);
3789}
3790
3791//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003792// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003793//===----------------------------------------------------------------------===//
3794
Chris Lattnerf22be932004-10-15 23:52:53 +00003795// Define these methods here so vtables don't get emitted into every translation
3796// unit that uses these classes.
3797
Pete Cooper75403d72015-06-24 20:22:23 +00003798GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003799 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003800}
3801
Pete Cooper75403d72015-06-24 20:22:23 +00003802BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003803 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003804}
3805
Pete Cooper75403d72015-06-24 20:22:23 +00003806FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003807 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003808}
3809
Pete Cooper75403d72015-06-24 20:22:23 +00003810ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003811 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003812}
3813
Pete Cooper75403d72015-06-24 20:22:23 +00003814ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003815 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003816}
3817
Pete Cooper75403d72015-06-24 20:22:23 +00003818InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003819 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003820}
3821
Pete Cooper75403d72015-06-24 20:22:23 +00003822AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003823 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3824 (Value *)getOperand(0), getAlignment());
3825 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3826 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003827}
3828
Pete Cooper75403d72015-06-24 20:22:23 +00003829LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003830 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3831 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003832}
3833
Pete Cooper75403d72015-06-24 20:22:23 +00003834StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003835 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003836 getAlignment(), getOrdering(), getSynchScope());
3837
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003838}
3839
Pete Cooper75403d72015-06-24 20:22:23 +00003840AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003841 AtomicCmpXchgInst *Result =
3842 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003843 getSuccessOrdering(), getFailureOrdering(),
3844 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003845 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003846 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003847 return Result;
3848}
3849
Pete Cooper75403d72015-06-24 20:22:23 +00003850AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003851 AtomicRMWInst *Result =
3852 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3853 getOrdering(), getSynchScope());
3854 Result->setVolatile(isVolatile());
3855 return Result;
3856}
3857
Pete Cooper75403d72015-06-24 20:22:23 +00003858FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003859 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3860}
3861
Pete Cooper75403d72015-06-24 20:22:23 +00003862TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003863 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003864}
3865
Pete Cooper75403d72015-06-24 20:22:23 +00003866ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003867 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003868}
3869
Pete Cooper75403d72015-06-24 20:22:23 +00003870SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003871 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003872}
3873
Pete Cooper75403d72015-06-24 20:22:23 +00003874FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003875 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003876}
3877
Pete Cooper75403d72015-06-24 20:22:23 +00003878FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003879 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003880}
3881
Pete Cooper75403d72015-06-24 20:22:23 +00003882UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003883 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003884}
3885
Pete Cooper75403d72015-06-24 20:22:23 +00003886SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003887 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003888}
3889
Pete Cooper75403d72015-06-24 20:22:23 +00003890FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003891 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003892}
3893
Pete Cooper75403d72015-06-24 20:22:23 +00003894FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003895 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003896}
3897
Pete Cooper75403d72015-06-24 20:22:23 +00003898PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003899 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003900}
3901
Pete Cooper75403d72015-06-24 20:22:23 +00003902IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003903 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003904}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003905
Pete Cooper75403d72015-06-24 20:22:23 +00003906BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003907 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003908}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003909
Pete Cooper75403d72015-06-24 20:22:23 +00003910AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003911 return new AddrSpaceCastInst(getOperand(0), getType());
3912}
3913
Pete Cooper75403d72015-06-24 20:22:23 +00003914CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003915 if (hasOperandBundles()) {
3916 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3917 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3918 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003919 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003920}
3921
Pete Cooper75403d72015-06-24 20:22:23 +00003922SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003923 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003924}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003925
Pete Cooper75403d72015-06-24 20:22:23 +00003926VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003927 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003928}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003929
Pete Cooper75403d72015-06-24 20:22:23 +00003930ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003931 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003932}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003933
Pete Cooper75403d72015-06-24 20:22:23 +00003934InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003935 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003936}
3937
Pete Cooper75403d72015-06-24 20:22:23 +00003938ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003939 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003940}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003941
Pete Cooper75403d72015-06-24 20:22:23 +00003942PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003943
Pete Cooper75403d72015-06-24 20:22:23 +00003944LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003945 return new LandingPadInst(*this);
3946}
3947
Pete Cooper75403d72015-06-24 20:22:23 +00003948ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003949 return new(getNumOperands()) ReturnInst(*this);
3950}
3951
Pete Cooper75403d72015-06-24 20:22:23 +00003952BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003953 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003954}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003955
Pete Cooper75403d72015-06-24 20:22:23 +00003956SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003957
Pete Cooper75403d72015-06-24 20:22:23 +00003958IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003959 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003960}
3961
Pete Cooper75403d72015-06-24 20:22:23 +00003962InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003963 if (hasOperandBundles()) {
3964 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3965 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3966 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003967 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003968}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003969
Pete Cooper75403d72015-06-24 20:22:23 +00003970ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003971
David Majnemer654e1302015-07-31 17:58:14 +00003972CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3973 return new (getNumOperands()) CleanupReturnInst(*this);
3974}
3975
David Majnemer654e1302015-07-31 17:58:14 +00003976CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003977 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003978}
3979
David Majnemer8a1c45d2015-12-12 05:38:55 +00003980CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3981 return new CatchSwitchInst(*this);
3982}
3983
3984FuncletPadInst *FuncletPadInst::cloneImpl() const {
3985 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003986}
3987
Pete Cooper75403d72015-06-24 20:22:23 +00003988UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003989 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003990 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003991}